{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:14.141388Z",
     "start_time": "2020-06-27T12:43:13.949388Z"
    }
   },
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:15.810488Z",
     "start_time": "2020-06-27T12:43:14.747100Z"
    }
   },
   "outputs": [],
   "source": [
    "from utils import *\n",
    "from nlp import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:16.907072Z",
     "start_time": "2020-06-27T12:43:16.732763Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:22.226344Z",
     "start_time": "2020-06-27T12:43:20.307222Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "\n",
    "fpath = 'qa_corpus.csv'\n",
    "content = pd.read_csv(fpath, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:23.935423Z",
     "start_time": "2020-06-27T12:43:23.035687Z"
    }
   },
   "outputs": [],
   "source": [
    "ltp = Ltp(seg=True, pos=True, seg_lexicon_path='data/lexicon_seg.txt', pos_lexicon_path='data/lexicon.txt')\n",
    "text_precess = ProcessText()\n",
    "stopwords = load_stop(['data/chinese_stopwords.txt', 'data/哈工大停用词表.txt'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# for idx, row in content.iterrows():\n",
    "#     if '您好' in row.question:\n",
    "#         print(row.question, row.answer, idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# len(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "content.to_csv('qa_corpus.csv', encoding='utf-8', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-18T12:55:46.904105Z",
     "start_time": "2020-06-18T12:55:46.702242Z"
    }
   },
   "outputs": [],
   "source": [
    "# 问答对中有的词，保留\n",
    "\n",
    "# def test_in_questions(string):\n",
    "#     for i, s in enumerate(content.question):\n",
    "#         if string == str(s).lower().strip():\n",
    "# #             print(i, s)\n",
    "# #             print(content.answer[i])\n",
    "#             return 1\n",
    "#     return 0\n",
    "\n",
    "# out = []\n",
    "# for i in stopwords:\n",
    "#     if test_in_questions(i):\n",
    "#         continue\n",
    "#     else:\n",
    "#         out.append(i)\n",
    "\n",
    "# with open('task_stopwords.txt', 'w', encoding='utf-8') as f:\n",
    "#     for w in out:\n",
    "#         f.write(w + '\\n')\n",
    "\n",
    "# stopwords = load_stop(['task_stopwords.txt'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:27.397031Z",
     "start_time": "2020-06-27T12:43:27.171059Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "代发 工资\n"
     ]
    }
   ],
   "source": [
    "# test\n",
    "q_content = text_precess(content.question[1], ltp.segmentor.segment)\n",
    "print(q_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:33.311691Z",
     "start_time": "2020-06-27T12:43:33.099663Z"
    }
   },
   "outputs": [],
   "source": [
    "def save_pickle(path, data):\n",
    "    # pickle不能保存lambda\n",
    "    fileObject = open(path, 'wb')\n",
    "    pickle.dump(data, fileObject)\n",
    "    fileObject.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## tf-idf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:39.476632Z",
     "start_time": "2020-06-27T12:43:36.115515Z"
    }
   },
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "\n",
    "f = partial(text_precess, cut_method=ltp.segmentor.segment)\n",
    "\n",
    "content['q_processed'] = content.question.apply(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:40.507553Z",
     "start_time": "2020-06-27T12:43:40.252552Z"
    }
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  qid question                                          answer q_processed\n0   1    补发网银盾  您可以到智慧柜员机办理此项业务，方便快捷，节省您的宝贵时间哦。若去柜台办理，请刷身份证取号。     补发 网银 盾\n1   2     代发工资         您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。       代发 工资\n2   3  对帐单查询打印         您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。  对 帐单 查询 打印\n3   4   理财产品取号                                     请说出您需要办理的业务    理财 产品 取号\n4   5     密码修改                     请您带上身份证到自助柜员机办理，简单快捷，操作容易哦。       密码 修改",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>qid</th>\n      <th>question</th>\n      <th>answer</th>\n      <th>q_processed</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>补发网银盾</td>\n      <td>您可以到智慧柜员机办理此项业务，方便快捷，节省您的宝贵时间哦。若去柜台办理，请刷身份证取号。</td>\n      <td>补发 网银 盾</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>代发工资</td>\n      <td>您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。</td>\n      <td>代发 工资</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>对帐单查询打印</td>\n      <td>您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。</td>\n      <td>对 帐单 查询 打印</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>理财产品取号</td>\n      <td>请说出您需要办理的业务</td>\n      <td>理财 产品 取号</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>密码修改</td>\n      <td>请您带上身份证到自助柜员机办理，简单快捷，操作容易哦。</td>\n      <td>密码 修改</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "content.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T09:29:54.718134Z",
     "start_time": "2020-06-19T09:29:54.373635Z"
    }
   },
   "outputs": [],
   "source": [
    "content.to_csv('qa_processed', encoding='utf-8', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T09:30:02.212088Z",
     "start_time": "2020-06-19T09:30:01.839625Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>qid</th>\n",
       "      <th>question</th>\n",
       "      <th>answer</th>\n",
       "      <th>q_processed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>补发网银盾</td>\n",
       "      <td>您可以到智慧柜员机办理此项业务，方便快捷，节省您的宝贵时间哦。若去柜台办理，请刷身份证取号。</td>\n",
       "      <td>补发 网银 盾</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>代发工资</td>\n",
       "      <td>您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。</td>\n",
       "      <td>代发 工资</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>对帐单查询打印</td>\n",
       "      <td>您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。</td>\n",
       "      <td>对 帐单 查询 打印</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>理财产品取号</td>\n",
       "      <td>请说出您需要办理的业务</td>\n",
       "      <td>理财 产品 取号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>密码修改</td>\n",
       "      <td>请您带上身份证到自助柜员机办理，简单快捷，操作容易哦。</td>\n",
       "      <td>密码 修改</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  qid question                                          answer q_processed\n",
       "0   1    补发网银盾  您可以到智慧柜员机办理此项业务，方便快捷，节省您的宝贵时间哦。若去柜台办理，请刷身份证取号。     补发 网银 盾\n",
       "1   2     代发工资         您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。       代发 工资\n",
       "2   3  对帐单查询打印         您要办理的是对公开户、销户、签约及其他业务。请刷身份证，小龙人来帮您取个号吧。  对 帐单 查询 打印\n",
       "3   4   理财产品取号                                     请说出您需要办理的业务    理财 产品 取号\n",
       "4   5     密码修改                     请您带上身份证到自助柜员机办理，简单快捷，操作容易哦。       密码 修改"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# content_1 = pd.read_csv('qa_processed', encoding='utf-8')\n",
    "# content_1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:43:45.792901Z",
     "start_time": "2020-06-27T12:43:45.339323Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\programdata\\envs\\nlplearning\\lib\\site-packages\\sklearn\\feature_extraction\\text.py:286: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['lex', 'δ', 'ψ', 'в', 'ⅲ', '＜δ', '［①ｂ］', '［③ｆ］', 'ａ', 'ｌｉ', 'ｒ．ｌ．', 'ｚｘｆｉｔｌ'] not in stop_words.\n",
      "  sorted(inconsistent))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10981\n",
      "(32526, 10981)\n"
     ]
    }
   ],
   "source": [
    "# 注意，自定义了analyzer，忽略了stopwords\n",
    "def split_func(s):\n",
    "    return s.split()\n",
    "\n",
    "vectorized = TfidfVectorizer(max_features=12000, tokenizer=split_func, stop_words=stopwords)\n",
    "X = vectorized.fit_transform(content['q_processed'])\n",
    "\n",
    "print(len(vectorized.vocabulary_))\n",
    "print(X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T12:45:46.852004Z",
     "start_time": "2020-06-27T12:45:46.558974Z"
    }
   },
   "outputs": [],
   "source": [
    "word_2_id = vectorized.vocabulary_\n",
    "\n",
    "save_pickle('data/tfidf_word2vec.bin', word_2_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:55:31.089253Z",
     "start_time": "2020-06-19T02:55:30.880003Z"
    }
   },
   "outputs": [],
   "source": [
    "vectorizer_path = 'model/tfidf.model'\n",
    "\n",
    "# pickle不能保存lambda\n",
    "fileObject = open(vectorizer_path, 'wb')\n",
    "pickle.dump(vectorized, fileObject)\n",
    "fileObject.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T13:43:41.379495Z",
     "start_time": "2020-06-19T13:43:41.132609Z"
    }
   },
   "outputs": [],
   "source": [
    "from copy import deepcopy\n",
    "\n",
    "word_2_id = deepcopy(vectorized.vocabulary_)\n",
    "id_2_word = {d: w for w, d in word_2_id.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:55:46.180790Z",
     "start_time": "2020-06-19T02:55:45.999462Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10981"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(word_2_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:56:01.760934Z",
     "start_time": "2020-06-19T02:56:01.544821Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11320\n"
     ]
    }
   ],
   "source": [
    "# tfidf的词表忽略了一些词，这些词在进行word vec之类的模型时，可以加入使用。Elmo等\n",
    "for q in content.q_processed:\n",
    "    for w in q.split():\n",
    "        if w not in word_2_id:\n",
    "            # print(\"adding\", w)\n",
    "            word_2_id[w] = len(word_2_id)\n",
    "            \n",
    "print(len(word_2_id))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:56:09.031317Z",
     "start_time": "2020-06-19T02:56:08.831669Z"
    }
   },
   "outputs": [],
   "source": [
    "# 单独保存全量词表\n",
    "save_pickle('data/full_word2id.bin', word_2_id)\n",
    "# save_pickle('data/full_id2word.bin', id_2_word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T13:44:04.387792Z",
     "start_time": "2020-06-19T13:44:00.702479Z"
    }
   },
   "outputs": [],
   "source": [
    "from operator import and_\n",
    "from functools import reduce\n",
    "\n",
    "# 倒排索引召回\n",
    "inverse_idx = X.transpose()\n",
    "\n",
    "and_pos_set = {'n', 'v', 'm', 'nh', 'ni', 'nl', 'ns', 'nt', 'ws'}\n",
    "\n",
    "\n",
    "def boolen_search(doc_word):\n",
    "    inverse_data = doc_word.transpose()\n",
    "    inverse_idx = []\n",
    "    for arr in inverse_data:\n",
    "        inverse_idx.append(set(np.nonzero(arr.toarray()[0])[0]))\n",
    "    return inverse_idx\n",
    "    \n",
    "\n",
    "inverse_table = boolen_search(X)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:06:52.561210Z",
     "start_time": "2020-06-19T16:06:52.239133Z"
    }
   },
   "outputs": [],
   "source": [
    "from dataclasses import dataclass, field\n",
    "from typing import Any\n",
    "\n",
    "@dataclass(order=True)\n",
    "class PrioritizedItem:\n",
    "    priority: int\n",
    "    item: Any=field(compare=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:37:24.448627Z",
     "start_time": "2020-06-19T16:37:24.159529Z"
    }
   },
   "outputs": [],
   "source": [
    "k_largest_heap = KLargest(10, [])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:45:15.228270Z",
     "start_time": "2020-06-19T16:45:15.007662Z"
    }
   },
   "outputs": [],
   "source": [
    "def search_tfidf(query):\n",
    "    \"\"\"\"\"\"\n",
    "    words = text_precess(query, ltp.segmentor.segment)\n",
    "    cws_list = words.split()\n",
    "    postags = ltp.postagger.postag(cws_list)\n",
    "\n",
    "\n",
    "    to_and = []\n",
    "    for i, postag in enumerate(postags):\n",
    "        if postag in and_pos_set:\n",
    "            to_and.append(cws_list[i])\n",
    "    print(to_and)\n",
    "    try:\n",
    "        candidates_ids = [word_2_id[w] for w in to_and]\n",
    "    except KeyError:\n",
    "        pass\n",
    "    \n",
    "    # 布尔搜索：找到同时包含关键词的文档\n",
    "    documents_ids = [\n",
    "         inverse_table[_id] for _id in candidates_ids\n",
    "    ]\n",
    "    merged_documents = reduce(and_, documents_ids)\n",
    "    \n",
    "    query_vec = vectorized.transform([words]).toarray()\n",
    "    # 计算相似性排序\n",
    "    k_largest_heap = KLargest(10, [])\n",
    "    for i in merged_documents:\n",
    "        a = PrioritizedItem(-distance(query_vec, X[i].toarray()), i)\n",
    "        k_largest_heap.add(a)\n",
    "        \n",
    "    sorted_docuemtns_id = [i.item for i in sorted(k_largest_heap.pool, reverse=True)]\n",
    "    \n",
    "#     sorted_docuemtns_id = sorted(merged_documents, key=lambda i: distance(query_vec, X[i].toarray()))\n",
    "#     a = [distance(query_vec, X[i].toarray()) for i in sorted_docuemtns_id]\n",
    "    \n",
    "    return sorted_docuemtns_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:45:16.306182Z",
     "start_time": "2020-06-19T16:45:16.096499Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['办理', '龙卡']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[32202, 344, 30939, 32362, 30934, 20038, 30928, 30914, 30938, 31033]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_tfidf('办理龙卡')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:35:59.797746Z",
     "start_time": "2020-06-19T16:35:59.566470Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['办理', '龙卡']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([32202,\n",
       "  344,\n",
       "  30939,\n",
       "  32362,\n",
       "  30934,\n",
       "  20038,\n",
       "  30928,\n",
       "  30914,\n",
       "  30938,\n",
       "  31033,\n",
       "  21590,\n",
       "  32090,\n",
       "  20446,\n",
       "  22636,\n",
       "  20035,\n",
       "  20563,\n",
       "  268,\n",
       "  20942,\n",
       "  22639,\n",
       "  22604,\n",
       "  20617,\n",
       "  21122,\n",
       "  21006,\n",
       "  22722,\n",
       "  22721,\n",
       "  20460,\n",
       "  21511,\n",
       "  30941,\n",
       "  32363,\n",
       "  22368,\n",
       "  20023,\n",
       "  22119,\n",
       "  20570,\n",
       "  22635,\n",
       "  23133,\n",
       "  22640,\n",
       "  20390,\n",
       "  22365],\n",
       " [0.341292547087244,\n",
       "  0.3538401266163863,\n",
       "  0.45032045652448693,\n",
       "  0.45032045652448693,\n",
       "  0.45448519447742286,\n",
       "  0.4921195827671576,\n",
       "  0.4934197202013858,\n",
       "  0.49372203293686745,\n",
       "  0.5100788000908579,\n",
       "  0.5122536771422572,\n",
       "  0.5174639945858079,\n",
       "  0.5371732140374923,\n",
       "  0.5461049088807657,\n",
       "  0.5585875607087916,\n",
       "  0.5668534532242864,\n",
       "  0.5673348366554356,\n",
       "  0.5683702898522947,\n",
       "  0.5743790597252916,\n",
       "  0.5775382465273065,\n",
       "  0.580778903873755,\n",
       "  0.5837367571160648,\n",
       "  0.5856567300619067,\n",
       "  0.5905027373441605,\n",
       "  0.6095259170543914,\n",
       "  0.6176184718625879,\n",
       "  0.6277104617783331,\n",
       "  0.6425604284835089,\n",
       "  0.6430452942959818,\n",
       "  0.6430452942959818,\n",
       "  0.6460643459286424,\n",
       "  0.6550009790513958,\n",
       "  0.6580775764655197,\n",
       "  0.6643881775696048,\n",
       "  0.7131328202548508,\n",
       "  0.7271124924860307,\n",
       "  0.7395264328939645,\n",
       "  0.7545993538710805,\n",
       "  0.7895195542288461])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_tfidf('办理龙卡')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:24.748204Z",
     "start_time": "2020-06-19T02:57:24.573669Z"
    }
   },
   "outputs": [],
   "source": [
    "save_pickle('data/doc_tfidf_vec.bin', X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:27.638579Z",
     "start_time": "2020-06-19T02:57:27.160326Z"
    }
   },
   "outputs": [],
   "source": [
    "save_pickle('data/inverse_idx_table.bin', inverse_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BM25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:37.991116Z",
     "start_time": "2020-06-19T02:57:37.815644Z"
    }
   },
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "from math import log\n",
    "\n",
    "def get_tf(sent_i, sent_j):\n",
    "    \"\"\"计算bm25的term frequence. sent来自预处理的sent_of_words列表。\"\"\"\n",
    "    freq = {}\n",
    "    sent_i_counts = Counter(sent_i)\n",
    "    # 计算i句中的词，在j句中的tf\n",
    "    for w in sent_j:\n",
    "        # if not self.filte_words(w_item):\n",
    "        #     continue\n",
    "        if w in sent_i_counts:\n",
    "            freq[w] = sent_i_counts[w]\n",
    "        else:\n",
    "            freq[w] = 0\n",
    "    total = len(sent_i)\n",
    "    return {word: count / total for word, count in freq.items()}\n",
    "\n",
    "\n",
    "def get_idf(content):\n",
    "    \"\"\"计算inverse document frequence. 这里计算句子的相似度，所以计算inverse sentence frequence\"\"\"\n",
    "    total_sent = len(content) + 1 # 假设有一个句子包含所有词\n",
    "    avg_len = 0\n",
    "    doc_freq = {}\n",
    "    for sent in content:\n",
    "        sent = sent.split()\n",
    "        avg_len += len(sent)\n",
    "        words = list({w for w in sent})\n",
    "        for word in words:\n",
    "            # 假设有一个句子包含所有词\n",
    "            count = doc_freq.setdefault(word, 1)\n",
    "            doc_freq[word] = count + 1\n",
    "    avg_len /= total_sent\n",
    "    # sklearn中的实现方式\n",
    "    idf = {word: log(total_sent / df) + 1 for word, df in doc_freq.items()}\n",
    "    return idf, avg_len\n",
    "\n",
    "\n",
    "def sent_corelation_func(sent_i, sent_j, idf, avg_len, k1=1.5, b=0.75):\n",
    "    \"\"\"计算bm25。\n",
    "\n",
    "    sent_i ： 与query对比的句子，在文档中进行遍历，D\n",
    "    sent_j : query的句子, Q\n",
    "    \"\"\"\n",
    "    tf = get_tf(sent_i, sent_j)\n",
    "\n",
    "    K = k1 * (1 - b + b * len(sent_i) / avg_len)\n",
    "    bm25 = 0\n",
    "    for j_word in sent_j:\n",
    "        bm25 += idf[j_word] * tf[j_word] * (k1 + 1) / (tf[j_word] + K)\n",
    "    return bm25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:41.797289Z",
     "start_time": "2020-06-19T02:57:41.532789Z"
    }
   },
   "outputs": [],
   "source": [
    "idf, avg_len = get_idf(content.q_processed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:51.227190Z",
     "start_time": "2020-06-19T02:57:51.055365Z"
    }
   },
   "outputs": [],
   "source": [
    "save_pickle('data/idf_and_avglen.bin', (idf, avg_len))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:54.230594Z",
     "start_time": "2020-06-19T02:57:54.058559Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.148760719175181"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sent_corelation_func(content.q_processed[21231].lower().split(), content.q_processed[21230].lower().split(), idf, avg_len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T02:57:57.594412Z",
     "start_time": "2020-06-19T02:57:57.407605Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qid             1\n",
      "question    补发网银盾\n",
      "Name: 0, dtype: object\n"
     ]
    }
   ],
   "source": [
    "for i, j in content[['qid', 'question']].iterrows():\n",
    "    print(j)\n",
    "    break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 词向量模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.models.fasttext import load_facebook_model\n",
    "\n",
    "\n",
    "fb_model  = load_facebook_model('cc.zh.300.bin')\n",
    "\n",
    "new_sent = list(map(lambda s: s.lower().split(), content.q_processed))\n",
    "len(new_sent)\n",
    "\n",
    "fb_model.build_vocab(new_sent, update=True)\n",
    "fb_model.train(sentences=new_sent, total_examples=len(new_sent), epochs=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected = []\n",
    "for w in word_2_id.keys():\n",
    "    vec = fb_model.wv[w]\n",
    "    selected.append((w, vec))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('selected_words_fasttext_300.txt', 'w', encoding='utf-8') as f:\n",
    "    first_line = str(len(selected)) + '\\t' + str(300)\n",
    "    f.write(first_line)\n",
    "    for w, v in selected:\n",
    "        line =  '\\n' + str(w) + '\\t' + array_to_string(v)\n",
    "        f.write(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T03:36:36.600553Z",
     "start_time": "2020-06-19T03:36:35.791399Z"
    }
   },
   "outputs": [],
   "source": [
    "# 上次处理，保存格式不对，所以再处理。。\n",
    "with open('selected_words_fasttext_300.txt', 'r', encoding='utf-8') as f:\n",
    "    with open('wordvec_fasttext_300.txt', 'w', encoding='utf-8') as ff:\n",
    "        file = f.readlines()\n",
    "        ff.write(file[0].replace('\\t', ' '))\n",
    "        for line in file[1: ]:\n",
    "            ff.write(line.replace('\\t', ' '))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T03:37:06.709576Z",
     "start_time": "2020-06-19T03:37:02.249797Z"
    }
   },
   "outputs": [],
   "source": [
    "from gensim.models import KeyedVectors\n",
    "\n",
    "word_vectors = KeyedVectors.load_word2vec_format('wordvec_fasttext_300.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然可以使用kmeans之类的聚类方法，但是此处利用annoy（基于树的向量聚类）进行稠密向量的索引和聚类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T07:40:39.053670Z",
     "start_time": "2020-06-19T07:40:38.828969Z"
    }
   },
   "outputs": [],
   "source": [
    "from annoy import AnnoyIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:26:41.000110Z",
     "start_time": "2020-06-19T08:26:40.790677Z"
    }
   },
   "outputs": [],
   "source": [
    "dim = 300\n",
    "\n",
    "index = AnnoyIndex(dim, 'angular')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:34:38.362694Z",
     "start_time": "2020-06-19T08:34:38.079871Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300,)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sent_avg_vec(content.q_processed[2], word_vectors).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:19:25.953249Z",
     "start_time": "2020-06-19T08:19:25.725822Z"
    }
   },
   "outputs": [],
   "source": [
    "from operator import or_\n",
    "\n",
    "\n",
    "def search_sentvec(query):\n",
    "    \"\"\"\"\"\"\n",
    "    words = text_precess(query, ltp.segmentor.segment)\n",
    "    cws_list = words.split()\n",
    "    postags = ltp.postagger.postag(cws_list)\n",
    "\n",
    "    to_and = []\n",
    "    for i, postag in enumerate(postags):\n",
    "        if postag in and_pos_set:\n",
    "            to_and.append(cws_list[i])\n",
    "    print(to_and)\n",
    "    \n",
    "    try:\n",
    "        candidates_ids = [word_2_id[w] for w in to_and]\n",
    "    except KeyError:\n",
    "        pass\n",
    "\n",
    "    documents_ids = [\n",
    "         set(np.nonzero(inverse_idx[_id].toarray()[0])[0]) for _id in candidates_ids\n",
    "    ]\n",
    "\n",
    "    merged_documents = reduce(or_, documents_ids)\n",
    "    sorted_docuemtns_id = sorted(merged_documents, \n",
    "                                 key=lambda i: distance(sent_avg_vec(cws_list, word_vectors), \n",
    "                                                                          sent_avg_vec(content.q_processed[i].split(), word_vectors)))\n",
    "\n",
    "    return sorted_docuemtns_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:19:35.291039Z",
     "start_time": "2020-06-19T08:19:34.911821Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['办理', '龙卡']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[344,\n",
       " 21590,\n",
       " 268,\n",
       " 20460,\n",
       " 20038,\n",
       " 30928,\n",
       " 265,\n",
       " 22188,\n",
       " 30939,\n",
       " 32362,\n",
       " 273,\n",
       " 30914,\n",
       " 20563,\n",
       " 30934,\n",
       " 20945,\n",
       " 757,\n",
       " 20942,\n",
       " 272,\n",
       " 21511,\n",
       " 21006,\n",
       " 21132,\n",
       " 22331,\n",
       " 20489,\n",
       " 392,\n",
       " 61,\n",
       " 20446,\n",
       " 20941,\n",
       " 20035,\n",
       " 269,\n",
       " 23101,\n",
       " 21768,\n",
       " 19945,\n",
       " 21457,\n",
       " 267,\n",
       " 274,\n",
       " 22721,\n",
       " 933,\n",
       " 20617,\n",
       " 22604,\n",
       " 22636,\n",
       " 22717,\n",
       " 22639,\n",
       " 22290,\n",
       " 20034,\n",
       " 346,\n",
       " 345,\n",
       " 32090,\n",
       " 32202,\n",
       " 20396,\n",
       " 22453,\n",
       " 22629,\n",
       " 31033,\n",
       " 22313,\n",
       " 22722,\n",
       " 969,\n",
       " 22273,\n",
       " 452,\n",
       " 30938,\n",
       " 21882,\n",
       " 19940,\n",
       " 21076,\n",
       " 42,\n",
       " 21099,\n",
       " 20365,\n",
       " 24636,\n",
       " 20337,\n",
       " 32299,\n",
       " 21336,\n",
       " 20805,\n",
       " 22452,\n",
       " 31341,\n",
       " 21617,\n",
       " 13326,\n",
       " 16106,\n",
       " 21335,\n",
       " 22244,\n",
       " 20348,\n",
       " 20565,\n",
       " 21122,\n",
       " 167,\n",
       " 30941,\n",
       " 32363,\n",
       " 22745,\n",
       " 22635,\n",
       " 22393,\n",
       " 21082,\n",
       " 20142,\n",
       " 30862,\n",
       " 21085,\n",
       " 23207,\n",
       " 20812,\n",
       " 31257,\n",
       " 30908,\n",
       " 23092,\n",
       " 19989,\n",
       " 22642,\n",
       " 20934,\n",
       " 22119,\n",
       " 22291,\n",
       " 20143,\n",
       " 23721,\n",
       " 19947,\n",
       " 20428,\n",
       " 20912,\n",
       " 19810,\n",
       " 20940,\n",
       " 22702,\n",
       " 20569,\n",
       " 22444,\n",
       " 20573,\n",
       " 20804,\n",
       " 20134,\n",
       " 22120,\n",
       " 20803,\n",
       " 19990,\n",
       " 21173,\n",
       " 32332,\n",
       " 22640,\n",
       " 21106,\n",
       " 20366,\n",
       " 266,\n",
       " 21052,\n",
       " 19960,\n",
       " 21842,\n",
       " 22451,\n",
       " 21337,\n",
       " 20140,\n",
       " 23410,\n",
       " 22243,\n",
       " 20493,\n",
       " 21610,\n",
       " 19872,\n",
       " 21135,\n",
       " 32191,\n",
       " 244,\n",
       " 31976,\n",
       " 20786,\n",
       " 20802,\n",
       " 22110,\n",
       " 23474,\n",
       " 20814,\n",
       " 22299,\n",
       " 21119,\n",
       " 580,\n",
       " 20021,\n",
       " 20145,\n",
       " 20482,\n",
       " 20362,\n",
       " 20570,\n",
       " 22445,\n",
       " 22368,\n",
       " 23095,\n",
       " 20390,\n",
       " 22663,\n",
       " 30844,\n",
       " 30902,\n",
       " 32080,\n",
       " 20135,\n",
       " 20315,\n",
       " 347,\n",
       " 23431,\n",
       " 23483,\n",
       " 23135,\n",
       " 20403,\n",
       " 22410,\n",
       " 21137,\n",
       " 23138,\n",
       " 22641,\n",
       " 22643,\n",
       " 20700,\n",
       " 30863,\n",
       " 770,\n",
       " 20338,\n",
       " 20411,\n",
       " 23411,\n",
       " 30867,\n",
       " 20496,\n",
       " 20546,\n",
       " 31995,\n",
       " 32200,\n",
       " 21019,\n",
       " 20465,\n",
       " 20036,\n",
       " 23960,\n",
       " 23421,\n",
       " 20408,\n",
       " 20545,\n",
       " 32067,\n",
       " 19907,\n",
       " 28651,\n",
       " 20567,\n",
       " 20023,\n",
       " 20535,\n",
       " 21024,\n",
       " 21081,\n",
       " 21175,\n",
       " 20029,\n",
       " 20181,\n",
       " 22664,\n",
       " 23423,\n",
       " 1025,\n",
       " 30895,\n",
       " 31970,\n",
       " 20045,\n",
       " 20989,\n",
       " 12415,\n",
       " 19852,\n",
       " 19873,\n",
       " 22277,\n",
       " 22644,\n",
       " 22679,\n",
       " 23695,\n",
       " 23259,\n",
       " 22492,\n",
       " 3732,\n",
       " 22334,\n",
       " 21599,\n",
       " 20560,\n",
       " 21020,\n",
       " 31221,\n",
       " 20141,\n",
       " 21925,\n",
       " 21134,\n",
       " 32037,\n",
       " 20796,\n",
       " 30957,\n",
       " 31968,\n",
       " 21924,\n",
       " 686,\n",
       " 20144,\n",
       " 31957,\n",
       " 23114,\n",
       " 21923,\n",
       " 20488,\n",
       " 22112,\n",
       " 32245,\n",
       " 25551,\n",
       " 23466,\n",
       " 21080,\n",
       " 23761,\n",
       " 21002,\n",
       " 22601,\n",
       " 31687,\n",
       " 22315,\n",
       " 31964,\n",
       " 22111,\n",
       " 25685,\n",
       " 22662,\n",
       " 24295,\n",
       " 32397,\n",
       " 23424,\n",
       " 24269,\n",
       " 32357,\n",
       " 30907,\n",
       " 31802,\n",
       " 20235,\n",
       " 22083,\n",
       " 32226,\n",
       " 20601,\n",
       " 23133,\n",
       " 22633,\n",
       " 23334,\n",
       " 30781,\n",
       " 20938,\n",
       " 453,\n",
       " 20518,\n",
       " 23396,\n",
       " 32070,\n",
       " 20179,\n",
       " 23289,\n",
       " 22326,\n",
       " 20387,\n",
       " 19851,\n",
       " 23496,\n",
       " 22446,\n",
       " 31225,\n",
       " 22637,\n",
       " 20125,\n",
       " 523,\n",
       " 21619,\n",
       " 21051,\n",
       " 32051,\n",
       " 22630,\n",
       " 406,\n",
       " 20466,\n",
       " 22783,\n",
       " 24977,\n",
       " 26058,\n",
       " 624,\n",
       " 20147,\n",
       " 23495,\n",
       " 24496,\n",
       " 26678,\n",
       " 21177,\n",
       " 22695,\n",
       " 19892,\n",
       " 22166,\n",
       " 22300,\n",
       " 20502,\n",
       " 19909,\n",
       " 22477,\n",
       " 21526,\n",
       " 23110,\n",
       " 20363,\n",
       " 21596,\n",
       " 20810,\n",
       " 166,\n",
       " 21174,\n",
       " 21113,\n",
       " 22389,\n",
       " 26059,\n",
       " 508,\n",
       " 20909,\n",
       " 21423,\n",
       " 21507,\n",
       " 119,\n",
       " 20388,\n",
       " 920,\n",
       " 20410,\n",
       " 19870,\n",
       " 26328,\n",
       " 21060,\n",
       " 22383,\n",
       " 20659,\n",
       " 32364,\n",
       " 30940,\n",
       " 23760,\n",
       " 24230,\n",
       " 20961,\n",
       " 20551,\n",
       " 20402,\n",
       " 19975,\n",
       " 776,\n",
       " 20916,\n",
       " 22316,\n",
       " 23290,\n",
       " 21443,\n",
       " 21532,\n",
       " 21101,\n",
       " 19869,\n",
       " 21178,\n",
       " 20600,\n",
       " 20653,\n",
       " 20655,\n",
       " 22946,\n",
       " 22049,\n",
       " 522,\n",
       " 30785,\n",
       " 25630,\n",
       " 26379,\n",
       " 31248,\n",
       " 32195,\n",
       " 20658,\n",
       " 22685,\n",
       " 23676,\n",
       " 23047,\n",
       " 24355,\n",
       " 20872,\n",
       " 208,\n",
       " 21102,\n",
       " 32033,\n",
       " 22369,\n",
       " 26056,\n",
       " 11984,\n",
       " 21199,\n",
       " 31677,\n",
       " 21315,\n",
       " 32180,\n",
       " 618,\n",
       " 20449,\n",
       " 20108,\n",
       " 318,\n",
       " 896,\n",
       " 22144,\n",
       " 19793,\n",
       " 24265,\n",
       " 21241,\n",
       " 23995,\n",
       " 26135,\n",
       " 20763,\n",
       " 30822,\n",
       " 20429,\n",
       " 22632,\n",
       " 24829,\n",
       " 32231,\n",
       " 21100,\n",
       " 23199,\n",
       " 21659,\n",
       " 21598,\n",
       " 12523,\n",
       " 19901,\n",
       " 32278,\n",
       " 858,\n",
       " 26228,\n",
       " 20593,\n",
       " 23640,\n",
       " 747,\n",
       " 20515,\n",
       " 946,\n",
       " 20732,\n",
       " 21932,\n",
       " 20721,\n",
       " 21521,\n",
       " 21258,\n",
       " 1001,\n",
       " 32237,\n",
       " 30850,\n",
       " 23606,\n",
       " 21658,\n",
       " 32064,\n",
       " 30909,\n",
       " 20391,\n",
       " 553,\n",
       " 20832,\n",
       " 21296,\n",
       " 21560,\n",
       " 21179,\n",
       " 25009,\n",
       " 22154,\n",
       " 20762,\n",
       " 31004,\n",
       " 21338,\n",
       " 21230,\n",
       " 21151,\n",
       " 32052,\n",
       " 20733,\n",
       " 20483,\n",
       " 20830,\n",
       " 25649,\n",
       " 20581,\n",
       " 31460,\n",
       " 19931,\n",
       " 12958,\n",
       " 26381,\n",
       " 32075,\n",
       " 32203,\n",
       " 20092,\n",
       " 25980,\n",
       " 24360,\n",
       " 31626,\n",
       " 23804,\n",
       " 25029,\n",
       " 26556,\n",
       " 31668,\n",
       " 24450,\n",
       " 20084,\n",
       " 31305,\n",
       " 19991,\n",
       " 23546,\n",
       " 20528,\n",
       " 678,\n",
       " 21695,\n",
       " 695,\n",
       " 20501,\n",
       " 21492,\n",
       " 20708,\n",
       " 22205,\n",
       " 23833,\n",
       " 22187,\n",
       " 451,\n",
       " 26176,\n",
       " 32356,\n",
       " 552,\n",
       " 32262,\n",
       " 22294,\n",
       " 19994,\n",
       " 1005,\n",
       " 20742,\n",
       " 32272,\n",
       " 20270,\n",
       " 31231,\n",
       " 26638,\n",
       " 21341,\n",
       " 20833,\n",
       " 23124,\n",
       " 21254,\n",
       " 30780,\n",
       " 32062,\n",
       " 21642,\n",
       " 23309,\n",
       " 25665,\n",
       " 750,\n",
       " 21103,\n",
       " 24614,\n",
       " 31052,\n",
       " 20710,\n",
       " 20627,\n",
       " 21049,\n",
       " 20398,\n",
       " 23907,\n",
       " 21661,\n",
       " 23896,\n",
       " 26075,\n",
       " 21933,\n",
       " 20585,\n",
       " 24973,\n",
       " 20811,\n",
       " 26108,\n",
       " 24477,\n",
       " 25540,\n",
       " 24082,\n",
       " 22438,\n",
       " 30992,\n",
       " 24460,\n",
       " 21881,\n",
       " 21048,\n",
       " 481,\n",
       " 21989,\n",
       " 26118,\n",
       " 987,\n",
       " 20831,\n",
       " 21496,\n",
       " 23794,\n",
       " 541,\n",
       " 20549,\n",
       " 20588,\n",
       " 459,\n",
       " 24408,\n",
       " 116,\n",
       " 24662,\n",
       " 21159,\n",
       " 20578,\n",
       " 20255,\n",
       " 23961,\n",
       " 21559,\n",
       " 21586,\n",
       " 25917,\n",
       " 24407,\n",
       " 25610,\n",
       " 26299,\n",
       " 20764,\n",
       " 20813,\n",
       " 26733,\n",
       " 23162,\n",
       " 25951,\n",
       " 22686,\n",
       " 20947,\n",
       " 19896,\n",
       " 12701,\n",
       " 23477,\n",
       " 30995,\n",
       " 23068,\n",
       " 21597,\n",
       " 3799,\n",
       " 22241,\n",
       " 32066,\n",
       " 23918,\n",
       " 24528,\n",
       " 20415,\n",
       " 809,\n",
       " 31770,\n",
       " 21478,\n",
       " 21288,\n",
       " 24326,\n",
       " 21930,\n",
       " 20829,\n",
       " 24660,\n",
       " 22251,\n",
       " 26070,\n",
       " 31686,\n",
       " 21629,\n",
       " 31963,\n",
       " 21815,\n",
       " 32074,\n",
       " 32071,\n",
       " 21075,\n",
       " 31470,\n",
       " 21510,\n",
       " 684,\n",
       " 25723,\n",
       " 20257,\n",
       " 21746,\n",
       " 31200,\n",
       " 31469,\n",
       " 23310,\n",
       " 24411,\n",
       " 31618,\n",
       " 22653,\n",
       " 31771,\n",
       " 32122,\n",
       " 20741,\n",
       " 21902,\n",
       " 23604,\n",
       " 24750,\n",
       " 22384,\n",
       " 985,\n",
       " 21866,\n",
       " 26705,\n",
       " 20962,\n",
       " 30925,\n",
       " 23326,\n",
       " 21290,\n",
       " 31051,\n",
       " 31009,\n",
       " 20218,\n",
       " 24540,\n",
       " 20756,\n",
       " 22366,\n",
       " 32046,\n",
       " 22706,\n",
       " 23717,\n",
       " 21683,\n",
       " 20048,\n",
       " 32029,\n",
       " 20433,\n",
       " 915,\n",
       " 21359,\n",
       " 861,\n",
       " 25155,\n",
       " 26469,\n",
       " 32123,\n",
       " 21728,\n",
       " 31684,\n",
       " 21324,\n",
       " 20587,\n",
       " 24967,\n",
       " 23387,\n",
       " 22034,\n",
       " 20738,\n",
       " 23049,\n",
       " 21498,\n",
       " 20028,\n",
       " 24076,\n",
       " 26109,\n",
       " 31001,\n",
       " 23773,\n",
       " 20525,\n",
       " 20755,\n",
       " 21209,\n",
       " 22784,\n",
       " 22994,\n",
       " 23088,\n",
       " 21698,\n",
       " 21512,\n",
       " 20120,\n",
       " 25747,\n",
       " 24847,\n",
       " 21555,\n",
       " 21146,\n",
       " 25542,\n",
       " 20577,\n",
       " 21702,\n",
       " 31621,\n",
       " 474,\n",
       " 24427,\n",
       " 25629,\n",
       " 26684,\n",
       " 20667,\n",
       " 22611,\n",
       " 26524,\n",
       " 31615,\n",
       " 30994,\n",
       " 22012,\n",
       " 21398,\n",
       " 24999,\n",
       " 32190,\n",
       " 20273,\n",
       " 22674,\n",
       " 25735,\n",
       " 738,\n",
       " 23041,\n",
       " 25353,\n",
       " 23725,\n",
       " 23996,\n",
       " 31539,\n",
       " 24086,\n",
       " 21725,\n",
       " 21408,\n",
       " 31803,\n",
       " 26149,\n",
       " 20524,\n",
       " 26539,\n",
       " 689,\n",
       " 21243,\n",
       " 32252,\n",
       " 20788,\n",
       " 24507,\n",
       " 21572,\n",
       " 21272,\n",
       " 26110,\n",
       " 23689,\n",
       " 22508,\n",
       " 923,\n",
       " 22024,\n",
       " 21628,\n",
       " 30905,\n",
       " 393,\n",
       " 25000,\n",
       " 31623,\n",
       " 21405,\n",
       " 24487,\n",
       " 24412,\n",
       " 405,\n",
       " 31551,\n",
       " 22890,\n",
       " 21870,\n",
       " 31550,\n",
       " 31172,\n",
       " 21759,\n",
       " 22377,\n",
       " 20603,\n",
       " 21745,\n",
       " 21222,\n",
       " 21788,\n",
       " 23378,\n",
       " 20437,\n",
       " 23200,\n",
       " 31616,\n",
       " 21005,\n",
       " 21903,\n",
       " 31342,\n",
       " 19863,\n",
       " 20119,\n",
       " 22385,\n",
       " 32371,\n",
       " 897,\n",
       " 24658,\n",
       " 21197,\n",
       " 20706,\n",
       " 20319,\n",
       " 26294,\n",
       " 31034,\n",
       " 24497,\n",
       " 21289,\n",
       " 32162,\n",
       " 30901,\n",
       " 21904,\n",
       " 25494,\n",
       " 24387,\n",
       " 22886,\n",
       " 22954,\n",
       " 32017,\n",
       " 138,\n",
       " 25137,\n",
       " 20976,\n",
       " 22342,\n",
       " 22268,\n",
       " 986,\n",
       " 24521,\n",
       " 20599,\n",
       " 25652,\n",
       " 21189,\n",
       " 21277,\n",
       " 25396,\n",
       " 30847,\n",
       " 22612,\n",
       " 23956,\n",
       " 21046,\n",
       " 20854,\n",
       " 21996,\n",
       " 25682,\n",
       " 475,\n",
       " 25087,\n",
       " 30904,\n",
       " 22202,\n",
       " 24419,\n",
       " 22805,\n",
       " 25995,\n",
       " 22650,\n",
       " 905,\n",
       " 1018,\n",
       " 117,\n",
       " 24485,\n",
       " 21124,\n",
       " 24657,\n",
       " 21528,\n",
       " 24731,\n",
       " 25493,\n",
       " 32031,\n",
       " 25978,\n",
       " 21477,\n",
       " 20531,\n",
       " 21561,\n",
       " 24718,\n",
       " 908,\n",
       " 21742,\n",
       " 21029,\n",
       " 32214,\n",
       " 30825,\n",
       " 148,\n",
       " 24498,\n",
       " 21287,\n",
       " 25397,\n",
       " 19969,\n",
       " 24615,\n",
       " 21905,\n",
       " 23223,\n",
       " 24516,\n",
       " 31045,\n",
       " 23377,\n",
       " 20550,\n",
       " 217,\n",
       " 31538,\n",
       " 24464,\n",
       " 25496,\n",
       " 21426,\n",
       " 23813,\n",
       " 22556,\n",
       " 22184,\n",
       " 31680,\n",
       " 20517,\n",
       " 20548,\n",
       " 31954,\n",
       " 25790,\n",
       " 20997,\n",
       " 21987,\n",
       " 22318,\n",
       " 32182,\n",
       " 22958,\n",
       " 22490,\n",
       " 22956,\n",
       " 23549,\n",
       " 558,\n",
       " 530,\n",
       " 31975,\n",
       " 22962,\n",
       " 26738,\n",
       " 26483,\n",
       " 20256,\n",
       " 21494,\n",
       " 171,\n",
       " 26131,\n",
       " 20670,\n",
       " 31737,\n",
       " 23367,\n",
       " 478,\n",
       " 24543,\n",
       " 25537,\n",
       " 22365,\n",
       " 22472,\n",
       " 23281,\n",
       " 24391,\n",
       " 20139,\n",
       " 22078,\n",
       " 31522,\n",
       " 30935,\n",
       " 21149,\n",
       " 20186,\n",
       " 24373,\n",
       " 21007,\n",
       " 23084,\n",
       " 25761,\n",
       " 23666,\n",
       " 30768,\n",
       " 24410,\n",
       " 20668,\n",
       " 23050,\n",
       " 31590,\n",
       " 32253,\n",
       " 25411,\n",
       " 25171,\n",
       " 31749,\n",
       " 370,\n",
       " 22408,\n",
       " 30977,\n",
       " 31377,\n",
       " 24898,\n",
       " 21744,\n",
       " 23035,\n",
       " 22286,\n",
       " 23585,\n",
       " 30993,\n",
       " 21148,\n",
       " 25487,\n",
       " 24397,\n",
       " 24794,\n",
       " 21154,\n",
       " 671,\n",
       " 22963,\n",
       " 22478,\n",
       " 24544,\n",
       " 906,\n",
       " 30915,\n",
       " 30856,\n",
       " 25955,\n",
       " 20526,\n",
       " 31018,\n",
       " 24087,\n",
       " 22730,\n",
       " 21034,\n",
       " 32258,\n",
       " 22175,\n",
       " 25052,\n",
       " 23532,\n",
       " 25395,\n",
       " 26406,\n",
       " 19854,\n",
       " 20383,\n",
       " 31007,\n",
       " 32133,\n",
       " 26450,\n",
       " 26730,\n",
       " 23382,\n",
       " 20519,\n",
       " 23544,\n",
       " 127,\n",
       " 598,\n",
       " 25371,\n",
       " 25356,\n",
       " 31491,\n",
       " 24374,\n",
       " 23810,\n",
       " 32008,\n",
       " 24909,\n",
       " 24520,\n",
       " 24499,\n",
       " 22959,\n",
       " 21273,\n",
       " 20385,\n",
       " 21790,\n",
       " 3211,\n",
       " 25264,\n",
       " 22964,\n",
       " 22683,\n",
       " 21829,\n",
       " 32030,\n",
       " 14891,\n",
       " 13876,\n",
       " 22108,\n",
       " 25412,\n",
       " 25756,\n",
       " 309,\n",
       " 20701,\n",
       " 23649,\n",
       " 22173,\n",
       " 22103,\n",
       " 31912,\n",
       " 32353,\n",
       " 21115,\n",
       " 21534,\n",
       " 22619,\n",
       " 20072,\n",
       " 31553,\n",
       " 25536,\n",
       " 23832,\n",
       " 20086,\n",
       " 21690,\n",
       " 25655,\n",
       " 26653,\n",
       " 20902,\n",
       " 310,\n",
       " 20669,\n",
       " 21979,\n",
       " 21891,\n",
       " 25766,\n",
       " 30767,\n",
       " 20787,\n",
       " 21065,\n",
       " 25628,\n",
       " 30851,\n",
       " 31040,\n",
       " 23812,\n",
       " 22960,\n",
       " 19837,\n",
       " 25474,\n",
       " 20867,\n",
       " 25001,\n",
       " 21544,\n",
       " 22054,\n",
       " 30903,\n",
       " 21237,\n",
       " 25929,\n",
       " 25941,\n",
       " 25524,\n",
       " 31908,\n",
       " 20834,\n",
       " 26407,\n",
       " 23177,\n",
       " 25759,\n",
       " 21878,\n",
       " 25758,\n",
       " 968,\n",
       " 688,\n",
       " 31909,\n",
       " 21691,\n",
       " 31670,\n",
       " 31049,\n",
       " 21576,\n",
       " 21991,\n",
       " 25789,\n",
       " 903,\n",
       " 21623,\n",
       " 31199,\n",
       " 22895,\n",
       " 22868,\n",
       " 493,\n",
       " 30913,\n",
       " 21236,\n",
       " 25937,\n",
       " 23099,\n",
       " 928,\n",
       " 31260,\n",
       " 23279,\n",
       " 21372,\n",
       " 22948,\n",
       " 20939]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_sentvec('办理龙卡')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:19:16.905891Z",
     "start_time": "2020-06-19T08:19:16.533930Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['办理', '龙卡']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[344,\n",
       " 21590,\n",
       " 268,\n",
       " 20460,\n",
       " 20038,\n",
       " 30928,\n",
       " 265,\n",
       " 22188,\n",
       " 30939,\n",
       " 32362,\n",
       " 273,\n",
       " 30914,\n",
       " 20563,\n",
       " 30934,\n",
       " 20945,\n",
       " 757,\n",
       " 20942,\n",
       " 272,\n",
       " 21511,\n",
       " 21006,\n",
       " 21132,\n",
       " 22331,\n",
       " 20489,\n",
       " 392,\n",
       " 61,\n",
       " 20446,\n",
       " 20941,\n",
       " 20035,\n",
       " 269,\n",
       " 23101,\n",
       " 21768,\n",
       " 19945,\n",
       " 21457,\n",
       " 267,\n",
       " 274,\n",
       " 22721,\n",
       " 933,\n",
       " 20617,\n",
       " 22604,\n",
       " 22636,\n",
       " 22717,\n",
       " 22639,\n",
       " 22290,\n",
       " 20034,\n",
       " 346,\n",
       " 345,\n",
       " 32090,\n",
       " 32202,\n",
       " 20396,\n",
       " 22453,\n",
       " 22629,\n",
       " 31033,\n",
       " 22313,\n",
       " 22722,\n",
       " 969,\n",
       " 22273,\n",
       " 452,\n",
       " 30938,\n",
       " 21882,\n",
       " 19940,\n",
       " 21076,\n",
       " 42,\n",
       " 21099,\n",
       " 20365,\n",
       " 24636,\n",
       " 20337,\n",
       " 32299,\n",
       " 21336,\n",
       " 20805,\n",
       " 22452,\n",
       " 31341,\n",
       " 21617,\n",
       " 13326,\n",
       " 16106,\n",
       " 21335,\n",
       " 22244,\n",
       " 20348,\n",
       " 20565,\n",
       " 21122,\n",
       " 167,\n",
       " 30941,\n",
       " 32363,\n",
       " 22745,\n",
       " 22635,\n",
       " 22393,\n",
       " 21082,\n",
       " 20142,\n",
       " 30862,\n",
       " 21085,\n",
       " 23207,\n",
       " 20812,\n",
       " 31257,\n",
       " 30908,\n",
       " 23092,\n",
       " 19989,\n",
       " 22642,\n",
       " 20934,\n",
       " 22119,\n",
       " 22291,\n",
       " 20143,\n",
       " 23721,\n",
       " 19947,\n",
       " 20428,\n",
       " 20912,\n",
       " 19810,\n",
       " 20940,\n",
       " 22702,\n",
       " 20569,\n",
       " 22444,\n",
       " 20573,\n",
       " 20804,\n",
       " 20134,\n",
       " 22120,\n",
       " 20803,\n",
       " 19990,\n",
       " 21173,\n",
       " 32332,\n",
       " 22640,\n",
       " 21106,\n",
       " 20366,\n",
       " 266,\n",
       " 21052,\n",
       " 19960,\n",
       " 21842,\n",
       " 22451,\n",
       " 21337,\n",
       " 20140,\n",
       " 23410,\n",
       " 22243,\n",
       " 20493,\n",
       " 21610,\n",
       " 19872,\n",
       " 21135,\n",
       " 32191,\n",
       " 244,\n",
       " 31976,\n",
       " 20786,\n",
       " 20802,\n",
       " 22110,\n",
       " 23474,\n",
       " 20814,\n",
       " 22299,\n",
       " 21119,\n",
       " 580,\n",
       " 20021,\n",
       " 20145,\n",
       " 20482,\n",
       " 20362,\n",
       " 20570,\n",
       " 22445,\n",
       " 22368,\n",
       " 23095,\n",
       " 20390,\n",
       " 22663,\n",
       " 30844,\n",
       " 30902,\n",
       " 32080,\n",
       " 20135,\n",
       " 20315,\n",
       " 347,\n",
       " 23431,\n",
       " 23483,\n",
       " 23135,\n",
       " 20403,\n",
       " 22410,\n",
       " 21137,\n",
       " 23138,\n",
       " 22641,\n",
       " 22643,\n",
       " 20700,\n",
       " 30863,\n",
       " 770,\n",
       " 20338,\n",
       " 20411,\n",
       " 23411,\n",
       " 30867,\n",
       " 20496,\n",
       " 20546,\n",
       " 31995,\n",
       " 32200,\n",
       " 21019,\n",
       " 20465,\n",
       " 20036,\n",
       " 23960,\n",
       " 23421,\n",
       " 20408,\n",
       " 20545,\n",
       " 32067,\n",
       " 19907,\n",
       " 28651,\n",
       " 20567,\n",
       " 20023,\n",
       " 20535,\n",
       " 21024,\n",
       " 21081,\n",
       " 21175,\n",
       " 20029,\n",
       " 20181,\n",
       " 22664,\n",
       " 23423,\n",
       " 1025,\n",
       " 30895,\n",
       " 31970,\n",
       " 20045,\n",
       " 20989,\n",
       " 12415,\n",
       " 19852,\n",
       " 19873,\n",
       " 22277,\n",
       " 22644,\n",
       " 22679,\n",
       " 23695,\n",
       " 23259,\n",
       " 22492,\n",
       " 3732,\n",
       " 22334,\n",
       " 21599,\n",
       " 20560,\n",
       " 21020,\n",
       " 31221,\n",
       " 20141,\n",
       " 21925,\n",
       " 21134,\n",
       " 32037,\n",
       " 20796,\n",
       " 30957,\n",
       " 31968,\n",
       " 21924,\n",
       " 686,\n",
       " 20144,\n",
       " 31957,\n",
       " 23114,\n",
       " 21923,\n",
       " 20488,\n",
       " 22112,\n",
       " 32245,\n",
       " 25551,\n",
       " 23466,\n",
       " 21080,\n",
       " 23761,\n",
       " 21002,\n",
       " 22601,\n",
       " 31687,\n",
       " 22315,\n",
       " 31964,\n",
       " 22111,\n",
       " 25685,\n",
       " 22662,\n",
       " 24295,\n",
       " 32397,\n",
       " 23424,\n",
       " 24269,\n",
       " 32357,\n",
       " 30907,\n",
       " 31802,\n",
       " 20235,\n",
       " 22083,\n",
       " 32226,\n",
       " 20601,\n",
       " 23133,\n",
       " 22633,\n",
       " 23334,\n",
       " 30781,\n",
       " 20938,\n",
       " 453,\n",
       " 20518,\n",
       " 23396,\n",
       " 32070,\n",
       " 20179,\n",
       " 23289,\n",
       " 22326,\n",
       " 20387,\n",
       " 19851,\n",
       " 23496,\n",
       " 22446,\n",
       " 31225,\n",
       " 22637,\n",
       " 20125,\n",
       " 523,\n",
       " 21619,\n",
       " 21051,\n",
       " 32051,\n",
       " 22630,\n",
       " 406,\n",
       " 20466,\n",
       " 22783,\n",
       " 24977,\n",
       " 26058,\n",
       " 624,\n",
       " 20147,\n",
       " 23495,\n",
       " 24496,\n",
       " 26678,\n",
       " 21177,\n",
       " 22695,\n",
       " 19892,\n",
       " 22166,\n",
       " 22300,\n",
       " 20502,\n",
       " 19909,\n",
       " 22477,\n",
       " 21526,\n",
       " 23110,\n",
       " 20363,\n",
       " 21596,\n",
       " 20810,\n",
       " 166,\n",
       " 21174,\n",
       " 21113,\n",
       " 22389,\n",
       " 26059,\n",
       " 508,\n",
       " 20909,\n",
       " 21423,\n",
       " 21507,\n",
       " 119,\n",
       " 20388,\n",
       " 920,\n",
       " 20410,\n",
       " 19870,\n",
       " 26328,\n",
       " 21060,\n",
       " 22383,\n",
       " 20659,\n",
       " 32364,\n",
       " 30940,\n",
       " 23760,\n",
       " 24230,\n",
       " 20961,\n",
       " 20551,\n",
       " 20402,\n",
       " 19975,\n",
       " 776,\n",
       " 20916,\n",
       " 22316,\n",
       " 23290,\n",
       " 21443,\n",
       " 21532,\n",
       " 21101,\n",
       " 19869,\n",
       " 21178,\n",
       " 20600,\n",
       " 20653,\n",
       " 20655,\n",
       " 22946,\n",
       " 22049,\n",
       " 522,\n",
       " 30785,\n",
       " 25630,\n",
       " 26379,\n",
       " 31248,\n",
       " 32195,\n",
       " 20658,\n",
       " 22685,\n",
       " 23676,\n",
       " 23047,\n",
       " 24355,\n",
       " 20872,\n",
       " 208,\n",
       " 21102,\n",
       " 32033,\n",
       " 22369,\n",
       " 26056,\n",
       " 11984,\n",
       " 21199,\n",
       " 31677,\n",
       " 21315,\n",
       " 32180,\n",
       " 618,\n",
       " 20449,\n",
       " 20108,\n",
       " 318,\n",
       " 896,\n",
       " 22144,\n",
       " 19793,\n",
       " 24265,\n",
       " 21241,\n",
       " 23995,\n",
       " 26135,\n",
       " 20763,\n",
       " 30822,\n",
       " 20429,\n",
       " 22632,\n",
       " 24829,\n",
       " 32231,\n",
       " 21100,\n",
       " 23199,\n",
       " 21659,\n",
       " 21598,\n",
       " 12523,\n",
       " 19901,\n",
       " 32278,\n",
       " 858,\n",
       " 26228,\n",
       " 20593,\n",
       " 23640,\n",
       " 747,\n",
       " 20515,\n",
       " 946,\n",
       " 20732,\n",
       " 21932,\n",
       " 20721,\n",
       " 21521,\n",
       " 21258,\n",
       " 1001,\n",
       " 32237,\n",
       " 30850,\n",
       " 23606,\n",
       " 21658,\n",
       " 32064,\n",
       " 30909,\n",
       " 20391,\n",
       " 553,\n",
       " 20832,\n",
       " 21296,\n",
       " 21560,\n",
       " 21179,\n",
       " 25009,\n",
       " 22154,\n",
       " 20762,\n",
       " 31004,\n",
       " 21338,\n",
       " 21230,\n",
       " 21151,\n",
       " 32052,\n",
       " 20733,\n",
       " 20483,\n",
       " 20830,\n",
       " 25649,\n",
       " 20581,\n",
       " 31460,\n",
       " 19931,\n",
       " 12958,\n",
       " 26381,\n",
       " 32075,\n",
       " 32203,\n",
       " 20092,\n",
       " 25980,\n",
       " 24360,\n",
       " 31626,\n",
       " 23804,\n",
       " 25029,\n",
       " 26556,\n",
       " 31668,\n",
       " 24450,\n",
       " 20084,\n",
       " 31305,\n",
       " 19991,\n",
       " 23546,\n",
       " 20528,\n",
       " 678,\n",
       " 21695,\n",
       " 695,\n",
       " 20501,\n",
       " 21492,\n",
       " 20708,\n",
       " 22205,\n",
       " 23833,\n",
       " 22187,\n",
       " 451,\n",
       " 26176,\n",
       " 32356,\n",
       " 552,\n",
       " 32262,\n",
       " 22294,\n",
       " 19994,\n",
       " 1005,\n",
       " 20742,\n",
       " 32272,\n",
       " 20270,\n",
       " 31231,\n",
       " 26638,\n",
       " 21341,\n",
       " 20833,\n",
       " 23124,\n",
       " 21254,\n",
       " 30780,\n",
       " 32062,\n",
       " 21642,\n",
       " 23309,\n",
       " 25665,\n",
       " 750,\n",
       " 21103,\n",
       " 24614,\n",
       " 31052,\n",
       " 20710,\n",
       " 20627,\n",
       " 21049,\n",
       " 20398,\n",
       " 23907,\n",
       " 21661,\n",
       " 23896,\n",
       " 26075,\n",
       " 21933,\n",
       " 20585,\n",
       " 24973,\n",
       " 20811,\n",
       " 26108,\n",
       " 24477,\n",
       " 25540,\n",
       " 24082,\n",
       " 22438,\n",
       " 30992,\n",
       " 24460,\n",
       " 21881,\n",
       " 21048,\n",
       " 481,\n",
       " 21989,\n",
       " 26118,\n",
       " 987,\n",
       " 20831,\n",
       " 21496,\n",
       " 23794,\n",
       " 541,\n",
       " 20549,\n",
       " 20588,\n",
       " 459,\n",
       " 24408,\n",
       " 116,\n",
       " 24662,\n",
       " 21159,\n",
       " 20578,\n",
       " 20255,\n",
       " 23961,\n",
       " 21559,\n",
       " 21586,\n",
       " 25917,\n",
       " 24407,\n",
       " 25610,\n",
       " 26299,\n",
       " 20764,\n",
       " 20813,\n",
       " 26733,\n",
       " 23162,\n",
       " 25951,\n",
       " 22686,\n",
       " 20947,\n",
       " 19896,\n",
       " 12701,\n",
       " 23477,\n",
       " 30995,\n",
       " 23068,\n",
       " 21597,\n",
       " 3799,\n",
       " 22241,\n",
       " 32066,\n",
       " 23918,\n",
       " 24528,\n",
       " 20415,\n",
       " 809,\n",
       " 31770,\n",
       " 21478,\n",
       " 21288,\n",
       " 24326,\n",
       " 21930,\n",
       " 20829,\n",
       " 24660,\n",
       " 22251,\n",
       " 26070,\n",
       " 31686,\n",
       " 21629,\n",
       " 31963,\n",
       " 21815,\n",
       " 32074,\n",
       " 32071,\n",
       " 21075,\n",
       " 31470,\n",
       " 21510,\n",
       " 684,\n",
       " 25723,\n",
       " 20257,\n",
       " 21746,\n",
       " 31200,\n",
       " 31469,\n",
       " 23310,\n",
       " 24411,\n",
       " 31618,\n",
       " 22653,\n",
       " 31771,\n",
       " 32122,\n",
       " 20741,\n",
       " 21902,\n",
       " 23604,\n",
       " 24750,\n",
       " 22384,\n",
       " 985,\n",
       " 21866,\n",
       " 26705,\n",
       " 20962,\n",
       " 30925,\n",
       " 23326,\n",
       " 21290,\n",
       " 31051,\n",
       " 31009,\n",
       " 20218,\n",
       " 24540,\n",
       " 20756,\n",
       " 22366,\n",
       " 32046,\n",
       " 22706,\n",
       " 23717,\n",
       " 21683,\n",
       " 20048,\n",
       " 32029,\n",
       " 20433,\n",
       " 915,\n",
       " 21359,\n",
       " 861,\n",
       " 25155,\n",
       " 26469,\n",
       " 32123,\n",
       " 21728,\n",
       " 31684,\n",
       " 21324,\n",
       " 20587,\n",
       " 24967,\n",
       " 23387,\n",
       " 22034,\n",
       " 20738,\n",
       " 23049,\n",
       " 21498,\n",
       " 20028,\n",
       " 24076,\n",
       " 26109,\n",
       " 31001,\n",
       " 23773,\n",
       " 20525,\n",
       " 20755,\n",
       " 21209,\n",
       " 22784,\n",
       " 22994,\n",
       " 23088,\n",
       " 21698,\n",
       " 21512,\n",
       " 20120,\n",
       " 25747,\n",
       " 24847,\n",
       " 21555,\n",
       " 21146,\n",
       " 25542,\n",
       " 20577,\n",
       " 21702,\n",
       " 31621,\n",
       " 474,\n",
       " 24427,\n",
       " 25629,\n",
       " 26684,\n",
       " 20667,\n",
       " 22611,\n",
       " 26524,\n",
       " 31615,\n",
       " 30994,\n",
       " 22012,\n",
       " 21398,\n",
       " 24999,\n",
       " 32190,\n",
       " 20273,\n",
       " 22674,\n",
       " 25735,\n",
       " 738,\n",
       " 23041,\n",
       " 25353,\n",
       " 23725,\n",
       " 23996,\n",
       " 31539,\n",
       " 24086,\n",
       " 21725,\n",
       " 21408,\n",
       " 31803,\n",
       " 26149,\n",
       " 20524,\n",
       " 26539,\n",
       " 689,\n",
       " 21243,\n",
       " 32252,\n",
       " 20788,\n",
       " 24507,\n",
       " 21572,\n",
       " 21272,\n",
       " 26110,\n",
       " 23689,\n",
       " 22508,\n",
       " 923,\n",
       " 22024,\n",
       " 21628,\n",
       " 30905,\n",
       " 393,\n",
       " 25000,\n",
       " 31623,\n",
       " 21405,\n",
       " 24487,\n",
       " 24412,\n",
       " 405,\n",
       " 31551,\n",
       " 22890,\n",
       " 21870,\n",
       " 31550,\n",
       " 31172,\n",
       " 21759,\n",
       " 22377,\n",
       " 20603,\n",
       " 21745,\n",
       " 21222,\n",
       " 21788,\n",
       " 23378,\n",
       " 20437,\n",
       " 23200,\n",
       " 31616,\n",
       " 21005,\n",
       " 21903,\n",
       " 31342,\n",
       " 19863,\n",
       " 20119,\n",
       " 22385,\n",
       " 32371,\n",
       " 897,\n",
       " 24658,\n",
       " 21197,\n",
       " 20706,\n",
       " 20319,\n",
       " 26294,\n",
       " 31034,\n",
       " 24497,\n",
       " 21289,\n",
       " 32162,\n",
       " 30901,\n",
       " 21904,\n",
       " 25494,\n",
       " 24387,\n",
       " 22886,\n",
       " 22954,\n",
       " 32017,\n",
       " 138,\n",
       " 25137,\n",
       " 20976,\n",
       " 22342,\n",
       " 22268,\n",
       " 986,\n",
       " 24521,\n",
       " 20599,\n",
       " 25652,\n",
       " 21189,\n",
       " 21277,\n",
       " 25396,\n",
       " 30847,\n",
       " 22612,\n",
       " 23956,\n",
       " 21046,\n",
       " 20854,\n",
       " 21996,\n",
       " 25682,\n",
       " 475,\n",
       " 25087,\n",
       " 30904,\n",
       " 22202,\n",
       " 24419,\n",
       " 22805,\n",
       " 25995,\n",
       " 22650,\n",
       " 905,\n",
       " 1018,\n",
       " 117,\n",
       " 24485,\n",
       " 21124,\n",
       " 24657,\n",
       " 21528,\n",
       " 24731,\n",
       " 25493,\n",
       " 32031,\n",
       " 25978,\n",
       " 21477,\n",
       " 20531,\n",
       " 21561,\n",
       " 24718,\n",
       " 908,\n",
       " 21742,\n",
       " 21029,\n",
       " 32214,\n",
       " 30825,\n",
       " 148,\n",
       " 24498,\n",
       " 21287,\n",
       " 25397,\n",
       " 19969,\n",
       " 24615,\n",
       " 21905,\n",
       " 23223,\n",
       " 24516,\n",
       " 31045,\n",
       " 23377,\n",
       " 20550,\n",
       " 217,\n",
       " 31538,\n",
       " 24464,\n",
       " 25496,\n",
       " 21426,\n",
       " 23813,\n",
       " 22556,\n",
       " 22184,\n",
       " 31680,\n",
       " 20517,\n",
       " 20548,\n",
       " 31954,\n",
       " 25790,\n",
       " 20997,\n",
       " 21987,\n",
       " 22318,\n",
       " 32182,\n",
       " 22958,\n",
       " 22490,\n",
       " 22956,\n",
       " 23549,\n",
       " 558,\n",
       " 530,\n",
       " 31975,\n",
       " 22962,\n",
       " 26738,\n",
       " 26483,\n",
       " 20256,\n",
       " 21494,\n",
       " 171,\n",
       " 26131,\n",
       " 20670,\n",
       " 31737,\n",
       " 23367,\n",
       " 478,\n",
       " 24543,\n",
       " 25537,\n",
       " 22365,\n",
       " 22472,\n",
       " 23281,\n",
       " 24391,\n",
       " 20139,\n",
       " 22078,\n",
       " 31522,\n",
       " 30935,\n",
       " 21149,\n",
       " 20186,\n",
       " 24373,\n",
       " 21007,\n",
       " 23084,\n",
       " 25761,\n",
       " 23666,\n",
       " 30768,\n",
       " 24410,\n",
       " 20668,\n",
       " 23050,\n",
       " 31590,\n",
       " 32253,\n",
       " 25411,\n",
       " 25171,\n",
       " 31749,\n",
       " 370,\n",
       " 22408,\n",
       " 30977,\n",
       " 31377,\n",
       " 24898,\n",
       " 21744,\n",
       " 23035,\n",
       " 22286,\n",
       " 23585,\n",
       " 30993,\n",
       " 21148,\n",
       " 25487,\n",
       " 24397,\n",
       " 24794,\n",
       " 21154,\n",
       " 671,\n",
       " 22963,\n",
       " 22478,\n",
       " 24544,\n",
       " 906,\n",
       " 30915,\n",
       " 30856,\n",
       " 25955,\n",
       " 20526,\n",
       " 31018,\n",
       " 24087,\n",
       " 22730,\n",
       " 21034,\n",
       " 32258,\n",
       " 22175,\n",
       " 25052,\n",
       " 23532,\n",
       " 25395,\n",
       " 26406,\n",
       " 19854,\n",
       " 20383,\n",
       " 31007,\n",
       " 32133,\n",
       " 26450,\n",
       " 26730,\n",
       " 23382,\n",
       " 20519,\n",
       " 23544,\n",
       " 127,\n",
       " 598,\n",
       " 25371,\n",
       " 25356,\n",
       " 31491,\n",
       " 24374,\n",
       " 23810,\n",
       " 32008,\n",
       " 24909,\n",
       " 24520,\n",
       " 24499,\n",
       " 22959,\n",
       " 21273,\n",
       " 20385,\n",
       " 21790,\n",
       " 3211,\n",
       " 25264,\n",
       " 22964,\n",
       " 22683,\n",
       " 21829,\n",
       " 32030,\n",
       " 14891,\n",
       " 13876,\n",
       " 22108,\n",
       " 25412,\n",
       " 25756,\n",
       " 309,\n",
       " 20701,\n",
       " 23649,\n",
       " 22173,\n",
       " 22103,\n",
       " 31912,\n",
       " 32353,\n",
       " 21115,\n",
       " 21534,\n",
       " 22619,\n",
       " 20072,\n",
       " 31553,\n",
       " 25536,\n",
       " 23832,\n",
       " 20086,\n",
       " 21690,\n",
       " 25655,\n",
       " 26653,\n",
       " 20902,\n",
       " 310,\n",
       " 20669,\n",
       " 21979,\n",
       " 21891,\n",
       " 25766,\n",
       " 30767,\n",
       " 20787,\n",
       " 21065,\n",
       " 25628,\n",
       " 30851,\n",
       " 31040,\n",
       " 23812,\n",
       " 22960,\n",
       " 19837,\n",
       " 25474,\n",
       " 20867,\n",
       " 25001,\n",
       " 21544,\n",
       " 22054,\n",
       " 30903,\n",
       " 21237,\n",
       " 25929,\n",
       " 25941,\n",
       " 25524,\n",
       " 31908,\n",
       " 20834,\n",
       " 26407,\n",
       " 23177,\n",
       " 25759,\n",
       " 21878,\n",
       " 25758,\n",
       " 968,\n",
       " 688,\n",
       " 31909,\n",
       " 21691,\n",
       " 31670,\n",
       " 31049,\n",
       " 21576,\n",
       " 21991,\n",
       " 25789,\n",
       " 903,\n",
       " 21623,\n",
       " 31199,\n",
       " 22895,\n",
       " 22868,\n",
       " 493,\n",
       " 30913,\n",
       " 21236,\n",
       " 25937,\n",
       " 23099,\n",
       " 928,\n",
       " 31260,\n",
       " 23279,\n",
       " 21372,\n",
       " 22948,\n",
       " 20939]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_sentvec('办理龙卡')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:14:02.450540Z",
     "start_time": "2020-06-19T08:14:01.951416Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['办理', '信用卡']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[969,\n",
       " 13326,\n",
       " 16106,\n",
       " 32299,\n",
       " 22745,\n",
       " 452,\n",
       " 30844,\n",
       " 20428,\n",
       " 244,\n",
       " 31957,\n",
       " 580,\n",
       " 20934,\n",
       " 20565,\n",
       " 31995,\n",
       " 22393,\n",
       " 24636,\n",
       " 31964,\n",
       " 32051,\n",
       " 20551,\n",
       " 32332,\n",
       " 31802,\n",
       " 32397,\n",
       " 21024,\n",
       " 1025,\n",
       " 624,\n",
       " 24496,\n",
       " 22783,\n",
       " 508,\n",
       " 19909,\n",
       " 30822,\n",
       " 20429,\n",
       " 31687,\n",
       " 23199,\n",
       " 770,\n",
       " 21619,\n",
       " 858,\n",
       " 19892,\n",
       " 11984,\n",
       " 20036,\n",
       " 32064,\n",
       " 20029,\n",
       " 32357,\n",
       " 23466,\n",
       " 20502,\n",
       " 32226,\n",
       " 32202,\n",
       " 30928,\n",
       " 31033,\n",
       " 23761,\n",
       " 21507,\n",
       " 32180,\n",
       " 23960,\n",
       " 32090,\n",
       " 24355,\n",
       " 23695,\n",
       " 32029,\n",
       " 20433,\n",
       " 32245,\n",
       " 21151,\n",
       " 32052,\n",
       " 30785,\n",
       " 20092,\n",
       " 20600,\n",
       " 20084,\n",
       " 32033,\n",
       " 24295,\n",
       " 19931,\n",
       " 23114,\n",
       " 19975,\n",
       " 30938,\n",
       " 21175,\n",
       " 406,\n",
       " 31460,\n",
       " 31668,\n",
       " 24076,\n",
       " 553,\n",
       " 24269,\n",
       " 22383,\n",
       " 32278,\n",
       " 208,\n",
       " 24829,\n",
       " 22111,\n",
       " 20528,\n",
       " 22112,\n",
       " 20446,\n",
       " 21134,\n",
       " 32070,\n",
       " 21521,\n",
       " 20810,\n",
       " 23110,\n",
       " 26638,\n",
       " 31626,\n",
       " 31677,\n",
       " 25685,\n",
       " 21100,\n",
       " 21102,\n",
       " 23760,\n",
       " 21932,\n",
       " 22946,\n",
       " 23804,\n",
       " 25540,\n",
       " 25917,\n",
       " 31305,\n",
       " 20763,\n",
       " 20733,\n",
       " 453,\n",
       " 21103,\n",
       " 23896,\n",
       " 344,\n",
       " 21924,\n",
       " 23833,\n",
       " 21179,\n",
       " 25610,\n",
       " 32062,\n",
       " 21923,\n",
       " 25551,\n",
       " 21590,\n",
       " 24082,\n",
       " 20035,\n",
       " 21572,\n",
       " 19994,\n",
       " 20796,\n",
       " 28651,\n",
       " 25723,\n",
       " 20549,\n",
       " 32272,\n",
       " 21925,\n",
       " 22477,\n",
       " 30914,\n",
       " 12958,\n",
       " 24360,\n",
       " 20501,\n",
       " 23640,\n",
       " 618,\n",
       " 523,\n",
       " 119,\n",
       " 31004,\n",
       " 24977,\n",
       " 30939,\n",
       " 32362,\n",
       " 23334,\n",
       " 31248,\n",
       " 21695,\n",
       " 31469,\n",
       " 1005,\n",
       " 26058,\n",
       " 19896,\n",
       " 20270,\n",
       " 21177,\n",
       " 12415,\n",
       " 24614,\n",
       " 20578,\n",
       " 522,\n",
       " 30780,\n",
       " 21423,\n",
       " 26328,\n",
       " 32037,\n",
       " 896,\n",
       " 32356,\n",
       " 268,\n",
       " 20179,\n",
       " 20563,\n",
       " 20872,\n",
       " 26059,\n",
       " 30904,\n",
       " 24477,\n",
       " 3799,\n",
       " 22805,\n",
       " 32075,\n",
       " 25630,\n",
       " 21241,\n",
       " 20947,\n",
       " 809,\n",
       " 776,\n",
       " 21122,\n",
       " 20909,\n",
       " 21101,\n",
       " 30934,\n",
       " 31470,\n",
       " 32046,\n",
       " 30925,\n",
       " 19901,\n",
       " 21178,\n",
       " 20363,\n",
       " 21341,\n",
       " 21113,\n",
       " 26228,\n",
       " 23290,\n",
       " 24662,\n",
       " 22604,\n",
       " 21496,\n",
       " 750,\n",
       " 31231,\n",
       " 25155,\n",
       " 22049,\n",
       " 24657,\n",
       " 20581,\n",
       " 21060,\n",
       " 20653,\n",
       " 23606,\n",
       " 20742,\n",
       " 20764,\n",
       " 552,\n",
       " 20732,\n",
       " 20588,\n",
       " 20762,\n",
       " 21989,\n",
       " 21174,\n",
       " 24450,\n",
       " 747,\n",
       " 22721,\n",
       " 478,\n",
       " 22012,\n",
       " 24967,\n",
       " 22144,\n",
       " 905,\n",
       " 20548,\n",
       " 20235,\n",
       " 22241,\n",
       " 23773,\n",
       " 30941,\n",
       " 32363,\n",
       " 20218,\n",
       " 20415,\n",
       " 26118,\n",
       " 21870,\n",
       " 23961,\n",
       " 31052,\n",
       " 21759,\n",
       " 21642,\n",
       " 25747,\n",
       " 20460,\n",
       " 21315,\n",
       " 22384,\n",
       " 1001,\n",
       " 24230,\n",
       " 31615,\n",
       " 31770,\n",
       " 22154,\n",
       " 24973,\n",
       " 116,\n",
       " 22611,\n",
       " 21258,\n",
       " 31051,\n",
       " 32231,\n",
       " 21338,\n",
       " 24387,\n",
       " 23725,\n",
       " 24521,\n",
       " 32214,\n",
       " 20961,\n",
       " 20398,\n",
       " 25353,\n",
       " 25652,\n",
       " 26108,\n",
       " 21049,\n",
       " 26678,\n",
       " 22890,\n",
       " 20708,\n",
       " 25629,\n",
       " 21659,\n",
       " 26556,\n",
       " 21159,\n",
       " 22639,\n",
       " 22722,\n",
       " 32371,\n",
       " 31771,\n",
       " 22385,\n",
       " 21290,\n",
       " 21296,\n",
       " 30909,\n",
       " 22784,\n",
       " 21658,\n",
       " 21933,\n",
       " 20599,\n",
       " 20273,\n",
       " 31618,\n",
       " 31034,\n",
       " 12523,\n",
       " 20585,\n",
       " 22024,\n",
       " 21560,\n",
       " 31686,\n",
       " 22632,\n",
       " 22342,\n",
       " 23717,\n",
       " 920,\n",
       " 23088,\n",
       " 30995,\n",
       " 22959,\n",
       " 20755,\n",
       " 25980,\n",
       " 1018,\n",
       " 25665,\n",
       " 20710,\n",
       " 22958,\n",
       " 22674,\n",
       " 20603,\n",
       " 20721,\n",
       " 22956,\n",
       " 3732,\n",
       " 21788,\n",
       " 678,\n",
       " 21492,\n",
       " 23133,\n",
       " 21746,\n",
       " 24528,\n",
       " 20038,\n",
       " 21048,\n",
       " 19824,\n",
       " 26176,\n",
       " 21288,\n",
       " 22612,\n",
       " 26135,\n",
       " 861,\n",
       " 22438,\n",
       " 22954,\n",
       " 30825,\n",
       " 24660,\n",
       " 31621,\n",
       " 22686,\n",
       " 30831,\n",
       " 30992,\n",
       " 20942,\n",
       " 21222,\n",
       " 23049,\n",
       " 30901,\n",
       " 22408,\n",
       " 451,\n",
       " 32066,\n",
       " 23377,\n",
       " 23676,\n",
       " 23918,\n",
       " 31007,\n",
       " 24411,\n",
       " 23068,\n",
       " 21902,\n",
       " 31616,\n",
       " 21698,\n",
       " 24408,\n",
       " 26294,\n",
       " 25649,\n",
       " 21702,\n",
       " 22508,\n",
       " 25496,\n",
       " 22706,\n",
       " 20655,\n",
       " 20587,\n",
       " 22964,\n",
       " 30850,\n",
       " 21398,\n",
       " 21018,\n",
       " 21661,\n",
       " 24464,\n",
       " 689,\n",
       " 23326,\n",
       " 897,\n",
       " 21046,\n",
       " 31803,\n",
       " 21230,\n",
       " 541,\n",
       " 23907,\n",
       " 19854,\n",
       " 26070,\n",
       " 24658,\n",
       " 23378,\n",
       " 21940,\n",
       " 21559,\n",
       " 20601,\n",
       " 20437,\n",
       " 24406,\n",
       " 21254,\n",
       " 459,\n",
       " 370,\n",
       " 32060,\n",
       " 21866,\n",
       " 31001,\n",
       " 25628,\n",
       " 24326,\n",
       " 20706,\n",
       " 21243,\n",
       " 31970,\n",
       " 20045,\n",
       " 23546,\n",
       " 21690,\n",
       " 24516,\n",
       " 23050,\n",
       " 20385,\n",
       " 22377,\n",
       " 21987,\n",
       " 21498,\n",
       " 22962,\n",
       " 22963,\n",
       " 21745,\n",
       " 31684,\n",
       " 31045,\n",
       " 23995,\n",
       " 25542,\n",
       " 32253,\n",
       " 26056,\n",
       " 22251,\n",
       " 22061,\n",
       " 23813,\n",
       " 21555,\n",
       " 24410,\n",
       " 30908,\n",
       " 21526,\n",
       " 22184,\n",
       " 24412,\n",
       " 21273,\n",
       " 31200,\n",
       " 32262,\n",
       " 22034,\n",
       " 22166,\n",
       " 24999,\n",
       " 31968,\n",
       " 20072,\n",
       " 25171,\n",
       " 23309,\n",
       " 23689,\n",
       " 20854,\n",
       " 21598,\n",
       " 24265,\n",
       " 26539,\n",
       " 32071,\n",
       " 31963,\n",
       " 20023,\n",
       " 32031,\n",
       " 30994,\n",
       " 21006,\n",
       " 21683,\n",
       " 23124,\n",
       " 21124,\n",
       " 21209,\n",
       " 22078,\n",
       " 20048,\n",
       " 21359,\n",
       " 946,\n",
       " 598,\n",
       " 25412,\n",
       " 20189,\n",
       " 26733,\n",
       " 23041,\n",
       " 24087,\n",
       " 21287,\n",
       " 581,\n",
       " 20741,\n",
       " 19919,\n",
       " 22103,\n",
       " 20567,\n",
       " 21815,\n",
       " 20701,\n",
       " 23649,\n",
       " 20436,\n",
       " 20462,\n",
       " 26684,\n",
       " 26524,\n",
       " 25397,\n",
       " 21443,\n",
       " 24373,\n",
       " 12701,\n",
       " 21189,\n",
       " 24498,\n",
       " 24507,\n",
       " 25537,\n",
       " 21996,\n",
       " 25411,\n",
       " 25493,\n",
       " 3211,\n",
       " 26075,\n",
       " 31342,\n",
       " 23832,\n",
       " 31590,\n",
       " 19969,\n",
       " 21728,\n",
       " 31737,\n",
       " 31538,\n",
       " 26406,\n",
       " 22960,\n",
       " 74,\n",
       " 21029,\n",
       " 21610,\n",
       " 24086,\n",
       " 24543,\n",
       " 32252,\n",
       " 22372,\n",
       " 22650,\n",
       " 30781,\n",
       " 19863,\n",
       " 25494,\n",
       " 21272,\n",
       " 20738,\n",
       " 23996,\n",
       " 23367,\n",
       " 21277,\n",
       " 26110,\n",
       " 21146,\n",
       " 24731,\n",
       " 30907,\n",
       " 24540,\n",
       " 22619,\n",
       " 23418,\n",
       " 25371,\n",
       " 22490,\n",
       " 22286,\n",
       " 21426,\n",
       " 31954,\n",
       " 31551,\n",
       " 23794,\n",
       " 24497,\n",
       " 21065,\n",
       " 490,\n",
       " 22202,\n",
       " 25655,\n",
       " 20962,\n",
       " 22397,\n",
       " 30940,\n",
       " 31036,\n",
       " 26381,\n",
       " 21149,\n",
       " 31975,\n",
       " 23200,\n",
       " 21534,\n",
       " 148,\n",
       " 20445,\n",
       " 31550,\n",
       " 738,\n",
       " 24909,\n",
       " 30833,\n",
       " 20617,\n",
       " 32162,\n",
       " 558,\n",
       " 24750,\n",
       " 31539,\n",
       " 25395,\n",
       " 25955,\n",
       " 25029,\n",
       " 22636,\n",
       " 20319,\n",
       " 19898,\n",
       " 25761,\n",
       " 32411,\n",
       " 23532,\n",
       " 30915,\n",
       " 20902,\n",
       " 20550,\n",
       " 23047,\n",
       " 21891,\n",
       " 21324,\n",
       " 20345,\n",
       " 21034,\n",
       " 22886,\n",
       " 30743,\n",
       " 22297,\n",
       " 20627,\n",
       " 20506,\n",
       " 21744,\n",
       " 31833,\n",
       " 21742,\n",
       " 20571,\n",
       " 32017,\n",
       " 21904,\n",
       " 21881,\n",
       " 21903,\n",
       " 684,\n",
       " 117,\n",
       " 23549,\n",
       " 31912,\n",
       " 32353,\n",
       " 19939,\n",
       " 31522,\n",
       " 26379,\n",
       " 20525,\n",
       " 20427,\n",
       " 19822,\n",
       " 20346,\n",
       " 26299,\n",
       " 20957,\n",
       " 19900,\n",
       " 25487,\n",
       " 25790,\n",
       " 217,\n",
       " 25396,\n",
       " 32122,\n",
       " 23812,\n",
       " 31908,\n",
       " 32058,\n",
       " 25951,\n",
       " 20356,\n",
       " 23281,\n",
       " 20360,\n",
       " 24391,\n",
       " 21408,\n",
       " 24407,\n",
       " 19899,\n",
       " 30935,\n",
       " 32074,\n",
       " 31009,\n",
       " 21979,\n",
       " 26469,\n",
       " 20531,\n",
       " 21323,\n",
       " 987,\n",
       " 23310,\n",
       " 22653,\n",
       " 21790,\n",
       " 24427,\n",
       " 24487,\n",
       " 21478,\n",
       " 32133,\n",
       " 22175,\n",
       " 23956,\n",
       " 20667,\n",
       " 26109,\n",
       " 20831,\n",
       " 24397,\n",
       " 22294,\n",
       " 22205,\n",
       " 30903,\n",
       " 20669,\n",
       " 32114,\n",
       " 31831,\n",
       " 21691,\n",
       " 30768,\n",
       " 20444,\n",
       " 20348,\n",
       " 24374,\n",
       " 32190,\n",
       " 26131,\n",
       " 31990,\n",
       " 25995,\n",
       " 21197,\n",
       " 26450,\n",
       " 24718,\n",
       " 32030,\n",
       " 20426,\n",
       " 26730,\n",
       " 30827,\n",
       " 22428,\n",
       " 30993,\n",
       " 31172,\n",
       " 22108,\n",
       " 19888,\n",
       " 25264,\n",
       " 31264,\n",
       " 20030,\n",
       " 21930,\n",
       " 22173,\n",
       " 30902,\n",
       " 318,\n",
       " 21494,\n",
       " 21725,\n",
       " 20041,\n",
       " 21991,\n",
       " 32237,\n",
       " 32008,\n",
       " 20786,\n",
       " 20830,\n",
       " 26705,\n",
       " 24460,\n",
       " 20028,\n",
       " 21586,\n",
       " 20556,\n",
       " 405,\n",
       " 21154,\n",
       " 20976,\n",
       " 20357,\n",
       " 985,\n",
       " 21075,\n",
       " 30977,\n",
       " 31377,\n",
       " 30856,\n",
       " 20867,\n",
       " 32057,\n",
       " 20431,\n",
       " 32182,\n",
       " 21405,\n",
       " 32300,\n",
       " 30761,\n",
       " 25474,\n",
       " 474,\n",
       " 21237,\n",
       " 31858,\n",
       " 24520,\n",
       " 19837,\n",
       " 21119,\n",
       " 22478,\n",
       " 30843,\n",
       " 671,\n",
       " 24615,\n",
       " 20670,\n",
       " 30767,\n",
       " 23338,\n",
       " 30847,\n",
       " 26149,\n",
       " 25356,\n",
       " 32258,\n",
       " 20916,\n",
       " 31261,\n",
       " 20572,\n",
       " 34,\n",
       " 923,\n",
       " 20120,\n",
       " 22556,\n",
       " 19962,\n",
       " 25009,\n",
       " 968,\n",
       " 20834,\n",
       " 21544,\n",
       " 19889,\n",
       " 25536,\n",
       " 20500,\n",
       " 25682,\n",
       " 20788,\n",
       " 19945,\n",
       " 21561,\n",
       " 25524,\n",
       " 20255,\n",
       " 22318,\n",
       " 23810,\n",
       " 30736,\n",
       " 393,\n",
       " 509,\n",
       " 26483,\n",
       " 19926,\n",
       " 21829,\n",
       " 20359,\n",
       " 986,\n",
       " 22268,\n",
       " 25137,\n",
       " 23035,\n",
       " 25978,\n",
       " 584,\n",
       " 906,\n",
       " 20577,\n",
       " 32364,\n",
       " 20186,\n",
       " 21115,\n",
       " 23177,\n",
       " 22541,\n",
       " 22730,\n",
       " 481,\n",
       " 31909,\n",
       " 26407,\n",
       " 20546,\n",
       " 32048,\n",
       " 31049,\n",
       " 30905,\n",
       " 21511,\n",
       " 859,\n",
       " 20829,\n",
       " 22404,\n",
       " 22494,\n",
       " 24485,\n",
       " 31623,\n",
       " 19968,\n",
       " 24544,\n",
       " 65,\n",
       " 133,\n",
       " 19815,\n",
       " 20668,\n",
       " 138,\n",
       " 32191,\n",
       " 31641,\n",
       " 13876,\n",
       " 14891,\n",
       " 30817,\n",
       " 31620,\n",
       " 24419,\n",
       " 20435,\n",
       " 21210,\n",
       " 19967,\n",
       " 24898,\n",
       " 489,\n",
       " 22368,\n",
       " 21477,\n",
       " 22994,\n",
       " 22683,\n",
       " 19963,\n",
       " 21236,\n",
       " 22602,\n",
       " 23604,\n",
       " 31199,\n",
       " 25735,\n",
       " 31619,\n",
       " 20935,\n",
       " 22561,\n",
       " 25087,\n",
       " 32059,\n",
       " 31617,\n",
       " 22868,\n",
       " 20526,\n",
       " 320,\n",
       " 22036,\n",
       " 24061,\n",
       " 22534,\n",
       " 949,\n",
       " 31692,\n",
       " 19769,\n",
       " 19932,\n",
       " 22472,\n",
       " 30816,\n",
       " 32123,\n",
       " 23382,\n",
       " 20037,\n",
       " 31018,\n",
       " 20417,\n",
       " 20461,\n",
       " 245,\n",
       " 21334,\n",
       " 22119,\n",
       " 26653,\n",
       " 26738,\n",
       " 31232,\n",
       " 21905,\n",
       " 24847,\n",
       " 23286,\n",
       " 19998,\n",
       " 30826,\n",
       " 31749,\n",
       " 31665,\n",
       " 23994,\n",
       " 20065,\n",
       " 20013,\n",
       " 21161,\n",
       " 32010,\n",
       " 688,\n",
       " 31250,\n",
       " 21471,\n",
       " 662,\n",
       " 31502,\n",
       " 31680,\n",
       " 23585,\n",
       " 319,\n",
       " 32359,\n",
       " 836,\n",
       " 30898,\n",
       " 31227,\n",
       " 23544,\n",
       " 20499,\n",
       " 21576,\n",
       " 32005,\n",
       " 127,\n",
       " 701,\n",
       " 30762,\n",
       " 24794,\n",
       " 21289,\n",
       " 21843,\n",
       " 19894,\n",
       " 20014,\n",
       " 20060,\n",
       " 20068,\n",
       " 20943,\n",
       " 940,\n",
       " 21623,\n",
       " 1017,\n",
       " 21628,\n",
       " 21090,\n",
       " 24321,\n",
       " 19935,\n",
       " 32067,\n",
       " 19907,\n",
       " 19890,\n",
       " 32089,\n",
       " 19902,\n",
       " 19915,\n",
       " 30829,\n",
       " 23113,\n",
       " 1037,\n",
       " 30911,\n",
       " 30867,\n",
       " 19827,\n",
       " 21039,\n",
       " 20485,\n",
       " 19959,\n",
       " 666,\n",
       " 30819,\n",
       " 22378,\n",
       " 22640,\n",
       " 20959,\n",
       " 21169,\n",
       " 23223,\n",
       " 20607,\n",
       " 31790,\n",
       " 31040,\n",
       " 396,\n",
       " 32027,\n",
       " 19807,\n",
       " 23986,\n",
       " 32056,\n",
       " 20454,\n",
       " 310,\n",
       " 20119,\n",
       " 31961,\n",
       " 32181,\n",
       " 20086,\n",
       " 30851,\n",
       " 20383,\n",
       " 20869,\n",
       " 31223,\n",
       " 25789,\n",
       " 19847,\n",
       " 23905,\n",
       " 21528,\n",
       " 20390,\n",
       " 530,\n",
       " 20486,\n",
       " 19921,\n",
       " 20535,\n",
       " 30823,\n",
       " 32203,\n",
       " 475,\n",
       " 20150,\n",
       " 25766,\n",
       " 30818,\n",
       " 19884,\n",
       " 19848,\n",
       " 19922,\n",
       " 22054,\n",
       " 19818,\n",
       " 30942,\n",
       " 23052,\n",
       " 25000,\n",
       " 20017,\n",
       " 20050,\n",
       " 22520,\n",
       " 30776,\n",
       " 30913,\n",
       " 19950,\n",
       " 31553,\n",
       " 22390,\n",
       " 23266,\n",
       " 30834,\n",
       " 20372,\n",
       " 22430,\n",
       " 31236,\n",
       " 21032,\n",
       " 21512,\n",
       " 32315,\n",
       " 23898,\n",
       " 24499,\n",
       " 31491,\n",
       " 22450,\n",
       " 31504,\n",
       " 19771,\n",
       " 33,\n",
       " 19973,\n",
       " 31670,\n",
       " 21025,\n",
       " 21148,\n",
       " 20746,\n",
       " 864,\n",
       " 31826,\n",
       " 21629,\n",
       " 22335,\n",
       " 20566,\n",
       " 25001,\n",
       " 20039,\n",
       " 23666,\n",
       " 30814,\n",
       " 20117,\n",
       " 204,\n",
       " 19845,\n",
       " 903,\n",
       " 19912,\n",
       " 915,\n",
       " 31341,\n",
       " 31950,\n",
       " 21335,\n",
       " 23099,\n",
       " 22610,\n",
       " 171,\n",
       " 30863,\n",
       " 30821,\n",
       " 32055,\n",
       " 214,\n",
       " 1062,\n",
       " 20452,\n",
       " 30842,\n",
       " 25052,\n",
       " 668,\n",
       " 835,\n",
       " 19897,\n",
       " 19960,\n",
       " 23256,\n",
       " 23505,\n",
       " 20132,\n",
       " 21878,\n",
       " 22542,\n",
       " 31030,\n",
       " 20010,\n",
       " 22531,\n",
       " 21136,\n",
       " 20596,\n",
       " 20747,\n",
       " 21336,\n",
       " 26434,\n",
       " 20026,\n",
       " 213,\n",
       " 9312,\n",
       " ...]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_sentvec('办理信用卡')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:09:11.503186Z",
     "start_time": "2020-06-19T08:09:11.278125Z"
    }
   },
   "outputs": [],
   "source": [
    "def show_content(idx):\n",
    "    print(content.iloc[idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:09:18.717172Z",
     "start_time": "2020-06-19T08:09:18.501175Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qid                                                          461\n",
      "question                                                 办理陆港通龙卡\n",
      "answer         客户申请陆港通龙卡，须本人前往柜台填写《陆港通龙卡服务申请表格》，同时须提供有效身份证件和地...\n",
      "q_processed                                            办理 陆港通 龙卡\n",
      "Name: 344, dtype: object\n"
     ]
    }
   ],
   "source": [
    "show_content(344)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:18:30.810115Z",
     "start_time": "2020-06-19T08:18:30.597764Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qid                                               51456\n",
      "question                             “华东师大龙卡名校卡”可否办理附属卡\n",
      "answer         “华东师大龙卡名校卡”有附属卡（学生身份持卡人除外），附属卡卡面采用标准卡卡面。\n",
      "q_processed                       华东师大 龙卡 名校 卡可否 办理 附属卡\n",
      "Name: 20460, dtype: object\n"
     ]
    }
   ],
   "source": [
    "show_content(20460)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:41:07.483022Z",
     "start_time": "2020-06-19T08:41:06.558443Z"
    }
   },
   "outputs": [],
   "source": [
    "vec_list = get_wordvec_dict(content.q_processed, word_vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:41:12.107366Z",
     "start_time": "2020-06-19T08:41:09.140Z"
    }
   },
   "outputs": [],
   "source": [
    "len(vec_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:39:53.426691Z",
     "start_time": "2020-06-19T08:39:53.216630Z"
    }
   },
   "outputs": [],
   "source": [
    "def build_annoy(n_trees=10):\n",
    "    for i, vec in enumerate(vec_list):\n",
    "        index.add_item(i, vec)\n",
    "    index.build(n_trees)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:41:18.947256Z",
     "start_time": "2020-06-19T08:41:13.743959Z"
    }
   },
   "outputs": [],
   "source": [
    "build_annoy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:43:39.843219Z",
     "start_time": "2020-06-19T08:43:39.618222Z"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:46:11.098535Z",
     "start_time": "2020-06-19T08:46:10.861506Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([344,\n",
       "  21590,\n",
       "  268,\n",
       "  20460,\n",
       "  20038,\n",
       "  30928,\n",
       "  265,\n",
       "  22188,\n",
       "  30939,\n",
       "  32362,\n",
       "  273,\n",
       "  30914,\n",
       "  20563,\n",
       "  30934,\n",
       "  757,\n",
       "  20945,\n",
       "  20942,\n",
       "  272,\n",
       "  21511,\n",
       "  21006],\n",
       " [0.0634312629699707,\n",
       "  0.3531995415687561,\n",
       "  0.406412810087204,\n",
       "  0.422742635011673,\n",
       "  0.42773884534835815,\n",
       "  0.43435052037239075,\n",
       "  0.4558327794075012,\n",
       "  0.4581727981567383,\n",
       "  0.4645462930202484,\n",
       "  0.4645462930202484,\n",
       "  0.4649970829486847,\n",
       "  0.46618008613586426,\n",
       "  0.4672144651412964,\n",
       "  0.46789371967315674,\n",
       "  0.47393569350242615,\n",
       "  0.47393569350242615,\n",
       "  0.4779311418533325,\n",
       "  0.4825846552848816,\n",
       "  0.48646438121795654,\n",
       "  0.4867269694805145])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test\n",
    "q = sent_avg_vec('办理 龙卡', word_vectors)\n",
    "\n",
    "index.get_nns_by_vector(q, 20, search_k=-1, include_distances=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到 annoy 返回结果是和暴力搜索的结果基本一致的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另一种采用kmeans预处理，划分搜索范围的方法，依赖于算法划分的准确度，一旦在某一个子集中搜索，不会在另一个子集中搜索。因为预处理时，是将kmeans预处理结果作为标签，保存下来的，所以固化了搜索范围。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T08:56:41.454643Z",
     "start_time": "2020-06-19T08:56:40.799763Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index.save('data/sentvec.ann')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T09:01:01.841746Z",
     "start_time": "2020-06-19T09:01:01.615737Z"
    }
   },
   "outputs": [],
   "source": [
    "def search_sentvec(query, top_k=20, include_distances=False):\n",
    "    words = text_precess(query, ltp.segmentor.segment)\n",
    "    \n",
    "    q_vec = sent_avg_vec(words, word_vectors)\n",
    "    \n",
    "    sorted_docuemtns_id = index.get_nns_by_vector(q_vec, top_k, search_k=-1, include_distances=include_distances)\n",
    "    \n",
    "    return sorted_docuemtns_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-19T16:27:27.905662Z",
     "start_time": "2020-06-19T16:27:27.639197Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PrioritizedItem(priority=0, item='a')\n",
      "[PrioritizedItem(priority=0, item='a')]\n",
      "PrioritizedItem(priority=0, item='a')\n",
      "[PrioritizedItem(priority=0, item='a'), PrioritizedItem(priority=1, item='a')]\n",
      "PrioritizedItem(priority=1, item='a')\n",
      "[PrioritizedItem(priority=1, item='a'), PrioritizedItem(priority=2, item='a')]\n",
      "PrioritizedItem(priority=2, item='a')\n",
      "[PrioritizedItem(priority=2, item='a'), PrioritizedItem(priority=3, item='a')]\n",
      "PrioritizedItem(priority=3, item='a')\n",
      "[PrioritizedItem(priority=3, item='a'), PrioritizedItem(priority=4, item='a')]\n",
      "PrioritizedItem(priority=4, item='a')\n",
      "[PrioritizedItem(priority=4, item='a'), PrioritizedItem(priority=5, item='a')]\n",
      "PrioritizedItem(priority=5, item='a')\n",
      "[PrioritizedItem(priority=5, item='a'), PrioritizedItem(priority=6, item='a')]\n",
      "PrioritizedItem(priority=6, item='a')\n",
      "[PrioritizedItem(priority=6, item='a'), PrioritizedItem(priority=7, item='a')]\n",
      "PrioritizedItem(priority=7, item='a')\n",
      "[PrioritizedItem(priority=7, item='a'), PrioritizedItem(priority=8, item='a')]\n",
      "PrioritizedItem(priority=8, item='a')\n",
      "[PrioritizedItem(priority=8, item='a'), PrioritizedItem(priority=9, item='a')]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "# 直接用库\n",
    "import heapq\n",
    "class KthLargest_2:\n",
    "    def __init__(self, k: int, nums):\n",
    "        self.pool = heapq.nlargest(k, nums)\n",
    "        heapq.heapify(self.pool)\n",
    "        self.k = k\n",
    "\n",
    "    def add(self, val: int) -> int:\n",
    "        if len(self.pool) < self.k:\n",
    "            heapq.heappush(self.pool,val)\n",
    "        else:\n",
    "            heapq.heappushpop(self.pool,val)\n",
    "        return self.pool[0]\n",
    "\n",
    "\n",
    "# Your KthLargest object will be instantiated and called as such:\n",
    "if __name__ == \"__main__\":\n",
    "    k = 2\n",
    "    nums = []\n",
    "    obj = KthLargest_2(k, nums)\n",
    "    for i in range(10):\n",
    "        a = PrioritizedItem(i, 'a')\n",
    "        param_1 = obj.add(a)\n",
    "        print(param_1)\n",
    "        print(obj.pool)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T10:03:44.343472Z",
     "start_time": "2020-06-22T10:03:44.311370Z"
    }
   },
   "outputs": [],
   "source": [
    "from bert_serving.client import BertClient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T10:03:59.351088Z",
     "start_time": "2020-06-22T10:03:45.144563Z"
    }
   },
   "outputs": [],
   "source": [
    "bc = BertClient()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T10:04:21.078734Z",
     "start_time": "2020-06-22T10:04:20.969826Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.6709697 , -0.11540446,  0.49079838, ...,  0.25540185,\n",
       "         0.57633656, -0.05261125],\n",
       "       [ 0.6727595 , -0.22872677,  0.34226033, ...,  0.305778  ,\n",
       "         0.35676366, -0.19390674],\n",
       "       [ 0.4310699 ,  0.11017738,  0.13510828, ..., -0.20400193,\n",
       "         0.0228704 ,  0.5595023 ]], dtype=float32)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bc.encode(['你好', '你好啊', '早上好'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T10:16:28.804088Z",
     "start_time": "2020-06-22T10:16:28.796552Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 2), ('1', '2')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(*[(1, '1'), (2, \"2\")]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Eval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T16:17:47.034749Z",
     "start_time": "2020-06-27T16:17:46.667739Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T16:18:15.473735Z",
     "start_time": "2020-06-27T16:18:15.243763Z"
    }
   },
   "outputs": [],
   "source": [
    "data = pd.read_csv('./data/eval_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-27T16:22:24.557301Z",
     "start_time": "2020-06-27T16:22:24.312322Z"
    }
   },
   "outputs": [],
   "source": [
    "def check_manul(data):\n",
    "    score = 0\n",
    "    for row in data.iterrows():\n",
    "        print(row)\n",
    "        v = int(input('对或错：'))\n",
    "        score += v\n",
    "    return score / 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2020-06-27T16:22:32.256Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, Unnamed: 0             0\n",
      "top0          我记忆力不好拉~呵呵\n",
      "top1              别老爱撒娇啊\n",
      "top2                你识字吗\n",
      "top3                学口语。\n",
      "top4               你有妈妈吗\n",
      "top5             梳头为啥能健脑\n",
      "top6          赞美是一种说话的艺术\n",
      "top7               你妈妈是谁\n",
      "top8              说话要有礼貌\n",
      "top9                  健谈\n",
      "input         我记忆力不好拉~呵呵\n",
      "model           my_model\n",
      "Name: 0, dtype: object)\n",
      "对或错：1\n",
      "(1, Unnamed: 0               1\n",
      "top0          下雪的原理是什么你知道吗\n",
      "top1          下雨的原理是什么你知道吗\n",
      "top2            你知道为什么会下雪吗\n",
      "top3            你知道为什么会下雨吗\n",
      "top4               你是啥都知道吗\n",
      "top5           今天的天气如何你知道吗\n",
      "top6           你知道今天是什么日子吗\n",
      "top7               你知道支付宝吗\n",
      "top8                  我知道啊\n",
      "top9            今天有什么事情要做的\n",
      "input         下雪的原理是什么你知道吗\n",
      "model             my_model\n",
      "Name: 1, dtype: object)\n",
      "对或错：1\n",
      "(2, Unnamed: 0                              2\n",
      "top0                           龙支付可以在哪里使用\n",
      "top1                            如何使用龙支付付款\n",
      "top2                        如何使用龙支付“收款”功能\n",
      "top3                       如何使用龙支付“向朋友付款”\n",
      "top4                       如何在善融商城使用龙支付购物\n",
      "top5                      更换了手机后是否影响龙支付使用\n",
      "top6          使用龙支付的“向朋友付款”，手机号/姓名填错了，怎么办\n",
      "top7                    动态口令卡最后一道口令能否用于交易\n",
      "top8                        毕业后能否继续使用大学生卡\n",
      "top9                      信用卡最早是哪个国家开始使用的\n",
      "input                          龙支付可以在哪里使用\n",
      "model                            my_model\n",
      "Name: 2, dtype: object)\n",
      "对或错：1\n",
      "(3, Unnamed: 0                    3\n",
      "top0                  回答200加300\n",
      "top1                马上回答200加300\n",
      "top2          马上回答一下200加300等于多少\n",
      "top3                    两百加三百等于\n",
      "top4                  两百加三百等于多少\n",
      "top5                      两百加三百\n",
      "top6                     你加一等于几\n",
      "top7                       一加一。\n",
      "top8                    一加一等于多少\n",
      "top9                     1加1等于几\n",
      "input                 回答200加300\n",
      "model                  my_model\n",
      "Name: 3, dtype: object)\n",
      "对或错：1\n",
      "(4, Unnamed: 0              4\n",
      "top0               我想买辆汽车\n",
      "top1               我想买辆跑车\n",
      "top2               我要去买辆车\n",
      "top3              我想买辆保时捷\n",
      "top4               我要去搞辆车\n",
      "top5                帮我叫辆车\n",
      "top6           你能帮我叫辆出租车吗\n",
      "top7            你帮我叫一辆出租车\n",
      "top8          你能帮我叫一辆出租车吗\n",
      "top9           好车，不过我可买不起\n",
      "input              我想买辆汽车\n",
      "model            my_model\n",
      "Name: 4, dtype: object)\n",
      "对或错：1\n",
      "(5, Unnamed: 0                  5\n",
      "top0                贷款还款的办理方法\n",
      "top1              网银贷款还款的办理方法\n",
      "top2            个人循环贷款放款的办理方法\n",
      "top3          个人循环贷款提前还款的办理方法\n",
      "top4          个人循环贷款逾期还款的办理方法\n",
      "top5                 贷款逾期还款方法\n",
      "top6              个人循环贷款的还款方法\n",
      "top7                 贷款其他还款方式\n",
      "top8                贷款还款时间的介绍\n",
      "top9                贷款还款方式的种类\n",
      "input               贷款还款的办理方法\n",
      "model                my_model\n",
      "Name: 5, dtype: object)\n",
      "对或错：1\n",
      "(6, Unnamed: 0              6\n",
      "top0           你知不知道怎么打电话\n",
      "top1               怎么才能知道\n",
      "top2           您怎么知道我不知道？\n",
      "top3           是吗？我怎么不知道？\n",
      "top4              你怎么知道的？\n",
      "top5          我怎么知道你说的是对的\n",
      "top6               咦你怎么知道\n",
      "top7               你怎么知道的\n",
      "top8               我怎么能知道\n",
      "top9               不知道怎么了\n",
      "input          你知不知道怎么打电话\n",
      "model            my_model\n",
      "Name: 6, dtype: object)\n",
      "对或错：1\n",
      "(7, Unnamed: 0                              7\n",
      "top0                   我的联系方式变更了，如何通知银行？ \n",
      "top1                  个人资料有变，是否可通过微信办理修改？\n",
      "top2                   如何通过网上变更信用卡账单寄送方式？\n",
      "top3              我如何通过微信办理约定账户设置（变更、取消）？\n",
      "top4                           如何取消账户变动通知\n",
      "top5          通过微信办理的约定账户设置（变更、取消）是否实时生效？\n",
      "top6              电子渠道办理业务时，为什么提示需要进行身份核实\n",
      "top7            客户在进行账户追加时是否可以对签约信息进行修改？ \n",
      "top8             是否支持账户变动通知服务的柜台集中签约和追加？ \n",
      "top9               贵行目前提供哪些渠道开通账户变动通知服务？ \n",
      "input                  我的联系方式变更了，如何通知银行？ \n",
      "model                            my_model\n",
      "Name: 7, dtype: object)\n",
      "对或错：1\n",
      "(8, Unnamed: 0             8\n",
      "top0          好呀！您想玩什么游戏\n",
      "top1             你想玩什么游戏\n",
      "top2          好啊玩什么呢，说说吧\n",
      "top3           怎么玩这个聊天室呀\n",
      "top4          乒乓球要怎么玩知道吗\n",
      "top5            你想聊什么都可以\n",
      "top6              你想吃点什么\n",
      "top7           你知道怎么玩微信吗\n",
      "top8            无聊什么都不想看\n",
      "top9            端午节可以吃什么\n",
      "input         好呀！您想玩什么游戏\n",
      "model           my_model\n",
      "Name: 8, dtype: object)\n",
      "对或错：1\n",
      "(9, Unnamed: 0                  9\n",
      "top0                 凯旋门是为谁建的\n",
      "top1                 凯旋门为谁而建的\n",
      "top2             巴黎凯旋门是为纪念谁而建\n",
      "top3               万里长城的是谁修建的\n",
      "top4                    谁建的长城\n",
      "top5                 谁修建了万里长城\n",
      "top6                  山城是哪座城市\n",
      "top7          哪座城市举办了2008年奥运会\n",
      "top8                   你家房子大吗\n",
      "top9                   房子卖的很火\n",
      "input                凯旋门是为谁建的\n",
      "model                my_model\n",
      "Name: 9, dtype: object)\n",
      "对或错：1\n",
      "(10, Unnamed: 0               10\n",
      "top0          非常抱歉，我不明白您的意思\n",
      "top1          非常抱歉，我不明白您的意思\n",
      "top2          非常抱歉，我不明白您的意思\n",
      "top3          非常抱歉，我不明白您的意思\n",
      "top4          非常抱歉，我不明白您的意思\n",
      "top5          非常抱歉，我不明白您的意思\n",
      "top6          非常抱歉，我不明白您的意思\n",
      "top7          非常抱歉，我不明白您的意思\n",
      "top8          非常抱歉，我不明白您的意思\n",
      "top9          非常抱歉，我不明白您的意思\n",
      "input                    对对\n",
      "model              my_model\n",
      "Name: 10, dtype: object)\n",
      "对或错：0\n",
      "(11, Unnamed: 0            11\n",
      "top0                直接下来\n",
      "top1                直接过来\n",
      "top2          直接点击就能进去了。\n",
      "top3                别停下来\n",
      "top4          直接问我问题就可以啦\n",
      "top5             你能不能下来弄\n",
      "top6               不要停下来\n",
      "top7             继续不要停下来\n",
      "top8             但当我之前过来\n",
      "top9          然后就没有然后了呀。\n",
      "input               直接下来\n",
      "model           my_model\n",
      "Name: 11, dtype: object)\n",
      "对或错：1\n",
      "(12, Unnamed: 0                  12\n",
      "top0                  你全家有多少人呀\n",
      "top1                      杀你全家\n",
      "top2                     死你们全家\n",
      "top3                    你生日是多少\n",
      "top4                人一生的喝水量是多少\n",
      "top5                  你每天做多少工作\n",
      "top6                地球上的鸟类有多少种\n",
      "top7              你可不可以陪我打羽毛球啊\n",
      "top8                你能否陪我一起打足球\n",
      "top9          地球表面有百分多少的地方被水覆盖\n",
      "input                 你全家有多少人呀\n",
      "model                 my_model\n",
      "Name: 12, dtype: object)\n",
      "对或错：1\n",
      "(13, Unnamed: 0               13\n",
      "top0          非常抱歉，我不明白您的意思\n",
      "top1          非常抱歉，我不明白您的意思\n",
      "top2          非常抱歉，我不明白您的意思\n",
      "top3          非常抱歉，我不明白您的意思\n",
      "top4          非常抱歉，我不明白您的意思\n",
      "top5          非常抱歉，我不明白您的意思\n",
      "top6          非常抱歉，我不明白您的意思\n",
      "top7          非常抱歉，我不明白您的意思\n",
      "top8          非常抱歉，我不明白您的意思\n",
      "top9          非常抱歉，我不明白您的意思\n",
      "input                  我是你呀\n",
      "model              my_model\n",
      "Name: 13, dtype: object)\n",
      "对或错：0\n",
      "(14, Unnamed: 0               14\n",
      "top0             妈妈说鼻子长才是漂亮\n",
      "top1                  你妈妈好丑\n",
      "top2               你妈妈漂亮不的啊\n",
      "top3           我要是不化妆的话漂亮吗？\n",
      "top4                 我长得好看吗\n",
      "top5          羞不羞这么人个人了还哭鼻子\n",
      "top6               我看你不怎么可爱\n",
      "top7            这么大人了还哭鼻子了啊\n",
      "top8           为什么人老了头发便会变白\n",
      "top9           为什么人老了头发就会变白\n",
      "input            妈妈说鼻子长才是漂亮\n",
      "model              my_model\n",
      "Name: 14, dtype: object)\n",
      "对或错：1\n",
      "(15, Unnamed: 0               15\n",
      "top0          非常抱歉，我不明白您的意思\n",
      "top1          非常抱歉，我不明白您的意思\n",
      "top2          非常抱歉，我不明白您的意思\n",
      "top3          非常抱歉，我不明白您的意思\n",
      "top4          非常抱歉，我不明白您的意思\n",
      "top5          非常抱歉，我不明白您的意思\n",
      "top6          非常抱歉，我不明白您的意思\n",
      "top7          非常抱歉，我不明白您的意思\n",
      "top8          非常抱歉，我不明白您的意思\n",
      "top9          非常抱歉，我不明白您的意思\n",
      "input                  你说谁啊\n",
      "model              my_model\n",
      "Name: 15, dtype: object)\n",
      "对或错：0\n",
      "(16, Unnamed: 0          16\n",
      "top0            这么不谦虚呀\n",
      "top1            你还挺不谦虚\n",
      "top2            你太不谦虚了\n",
      "top3             你太谦虚了\n",
      "top4             这么客气啊\n",
      "top5             你还挺谦虚\n",
      "top6           跟我还这么客气\n",
      "top7          你不能这么没礼貌\n",
      "top8           这么客气干什么\n",
      "top9             一点不谦虚\n",
      "input           这么不谦虚呀\n",
      "model         my_model\n",
      "Name: 16, dtype: object)\n",
      "对或错：1\n",
      "(17, Unnamed: 0             17\n",
      "top0                 我好无赖\n",
      "top1                  你赖皮\n",
      "top2              你怎么这么无赖\n",
      "top3                 你是坏蛋\n",
      "top4                 你不讲理\n",
      "top5          我不，我要撒娇，耍赖，\n",
      "top6           烦我吗？我这么可爱！\n",
      "top7           真的吗?你喜欢被我骂\n",
      "top8                讨厌我邻居\n",
      "top9               我讨厌机器人\n",
      "input                我好无赖\n",
      "model            my_model\n",
      "Name: 17, dtype: object)\n"
     ]
    }
   ],
   "source": [
    "check_manul(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "oldHeight": 183,
   "position": {
    "height": "40px",
    "left": "733px",
    "right": "20px",
    "top": "120px",
    "width": "283px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "varInspector_section_display": "none",
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}